Dea*_*orm 5 php wordpress templates subdirectory
不久前,我开始了一个新的Wordpress项目。但是我遇到了一个问题。由于存在多种设计,因此我需要为不同页面模板上的页面,帖子和文本格式输出创建多个模板。
因此,由于模板文件太多,因此我想创建一些子目录。我知道自Wordpress 3.4及更高版本起,您就可page-templates
以为所有页面模板使用子目录名称,但是我如何将其用于格式文件和发布文件。
是的,我确实尝试添加以下功能:
get_template_part('/template-parts/page-templates' , 'page');
Run Code Online (Sandbox Code Playgroud)
和
require( get_template_directory() . '/template-parts/template-tags.php' );
Run Code Online (Sandbox Code Playgroud)
我要创建的理想目录结构如下:
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
Run Code Online (Sandbox Code Playgroud)
所以要明确。我想为上述模板文件创建结构。不要介意诸如此类的文件夹CSS
。这些文件夹设置正确。目的是在我成功创建结构之后,能够从/wp-admin
编辑页面部分中选择模板,例如页面模板。
小智 7
WP_Theme
get_page_templates()
类包含提供此过滤器的方法:
apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
请记住,在 wordpresspost
和page
are post_type
s 中,
add_filter( "theme_post_templates", ...
并且add_filter( "theme_page_templates",...
应该是有效的。
在该方法的法典“使用者”部分下,它指出:
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.
Run Code Online (Sandbox Code Playgroud)
我不确定相对 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' );
Run Code Online (Sandbox Code Playgroud)
和/或
add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
Run Code Online (Sandbox Code Playgroud)