Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress template files in subdirectory's

A while ago i started a new Wordpress project. But i got into an issue. Since there are multiple designs i need to create multiple templates for pages, posts and text format outputs for on the different page-templates.

So since the're are so many template files i wanted to create some subdirectory's. i know that since Wordpress 3.4 and higher you're able to use the subdirectory name page-templates for all the page templates but how could i use that for the format files and for the post files.

Yes i did try to add functions like:

 get_template_part('/template-parts/page-templates' , 'page');

And

require( get_template_directory() . '/template-parts/template-tags.php' );

The ideal directory structure i would like to create is as follows:

wp-content/themes/mytheme
- archive
- 404
- CSS
- JS
- Images 
- template-parts (dir)
-- page-templates (dir for page-template files.)
-- format-templates (dir for format-templates.)
-- post-templates (dir for post-templates.)
- header
- footer

So to be clear. I want to create the structure for template files like above. Don't mind the folders like CSS etc. Those are set the correct way. The intention is, after i've succesfully created the structure, to be able to select the templates like a page temple from the /wp-admin edit page section.

like image 423
Deathstorm Avatar asked May 30 '17 12:05

Deathstorm


People also ask

Where are WordPress templates stored?

How Do You Find Templates in WordPress? Templates are files stored on the server of your WordPress hosting provider. If you wish to edit your templates, then you will need to know how to access them. One way to find your theme's templates is by using an FTP client like FileZilla.

In what folder should a new custom theme folder be located?

Theme folder and file structureIn block themes, templates must be placed inside a folder called templates, and all template parts must be placed inside a folder called parts. style. css should reside in the root directory of your theme not within the CSS directory.

How do I access WordPress theme files?

Access the theme code editor via Appearance -> Theme File Editor. On the right side, it will display a list of template files the theme includes. Click on any file to see its content and make changes.

How do I find my WordPress files?

Well, simply just go to your WordPress Dashboard. If you wish to make changes to your Theme, then navigate to wp-admin -> Appearance -> Editor. Afterward, you will be able to view the files inside your Theme.


2 Answers

WP_Theme class contains the method get_page_templates() that offers this filter:

apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );

line 1103 of class-wp-theme.php

Keeping in mind that in wordpress post and page are post_types,

add_filter( "theme_post_templates", ... and add_filter( "theme_page_templates",... should be valid.

Under the codex's "Used By" section for that method, it states:

wp-admin/includes/template.php: page_template_dropdown()

wp-admin/includes/meta-boxes.php: page_attributes_meta_box()

Which leads me to believe it would make them available on the /wp-admin/ edit page section.

Info from core files regarding the filter:

 * Filters list of page templates for a theme.
 *
 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
 * @since 4.7.0 Added the `$post_type` parameter.
 *
 * @param array        $post_templates Array of page templates. Keys are filenames,
 *                                     values are translated names.
 * @param WP_Theme     $this           The theme object.
 * @param WP_Post|null $post           The post being edited, provided for context, or null.
 * @param string       $post_type      Post type to get the templates for.

I'm unsure if a relative uri works here or if you need get_theme_file_path(), but am assuming the former.

function deep_templates( $post_templates, $theme, $post, $post_type )
   $post_templates['/folder/folder/folder/file.php'] = "Page Style One";
   $post_templates['/folder/other-folder/file.php']  = "Page Style Two";
   return $post_templates;
add_filter( 'theme_page_templates', 'deep_templates' );

AND/OR

add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
like image 52
hwl Avatar answered Oct 11 '22 14:10

hwl


Here is an example of what you need to do.

- directory1 (dir)
-- directory2 (dir)
--- template-file.php

To access this template with get_template_part you would do this:

get_template_part( 'directory1/directory2/template', 'file' );

'directory1/directory2 defines the directory structure. template' is the string before the - in the template name. 'file' is the string after the dash in the template name.

So in your case if you had a template file called page-contact.php in your page-templates directory, you would use.

get_template_part('template-parts/page-templates/page' , 'contact');

like image 21
WizardCoder Avatar answered Oct 11 '22 13:10

WizardCoder