Wordpress Template Files in a Subdirectory

Some time ago I started a new Wordpress project. But I got into a problem. Since there are several projects, I need to create several templates for pages, messages and text format outputs for different page templates.

So, since I have so many template files, I wanted to create some subdirectory. I know that with Wordpress 3.4 and above you can use the subdirectory name page-templatesfor all page templates, but how can I use this for format files and for message files.

Yes, I tried to add features 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

, . , . CSS .. . , , , , , /wp-admin.

+4
4

WP_Theme get_page_templates(), :

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

1103 -wp-theme.php

, wordpress post page post_type s,

add_filter( "theme_post_templates", ... add_filter( "theme_page_templates",... .

codex "Used By" :

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

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

, /wp-admin/.

, :

 * 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.

, uri get_theme_file_path(), , .

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' );

/

add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
+1

, .

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

get_template_part, :

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

'directory1/directory2 . template' - - . 'file' - dash .

, page-contact.php page-templates, .

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

+2

get_template_part WordPress THEME.

, WordPress ( wp-content/themes/), PHP, CSS .., .

, WordPress, .

-1

( "page-templates" ) , .

, de Backend (wp-admin), :

<?php
/*
Template Name: TEMPLATE-NAME
*/
?>

at the top of each file (template). Then inside each file you can add blocks or modules of the project using the get_template_part () function.

-1
source

Source: https://habr.com/ru/post/1678170/


All Articles