子目录中的 Wordpress 模板文件
Wordpress template files in subdirectory's
不久前我开始了一个新的 Wordpress 项目。但是我遇到了一个问题。由于有多种设计,我需要为页面创建多个模板,posts 和不同页面模板上的文本格式输出。
由于模板文件太多,我想创建一些子目录。我知道自 Wordpress 3.4 及更高版本以来,您可以将子目录名称 page-templates
用于所有页面模板,但我如何将其用于格式文件和 post 文件。
是的,我确实尝试添加如下功能:
get_template_part('/template-parts/page-templates' , 'page');
和
require( get_template_directory() . '/template-parts/template-tags.php' );
我想创建的理想目录结构如下:
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
等文件夹。这些文件夹设置正确。我的目的是,在我成功创建结构后,能够 select 像 /wp-admin
编辑页面部分中的页面模板一样的模板。
这是您需要执行的操作的示例。
- directory1 (dir)
-- directory2 (dir)
--- template-file.php
要使用 get_template_part
访问此模板,您可以这样做:
get_template_part( 'directory1/directory2/template', 'file' );
'directory1/directory2
定义目录结构。
template'
是模板名称中 -
之前的字符串。
'file'是模板名称中dash
后面的字符串。
因此在您的情况下,如果您的 page-templates
目录中有一个名为 page-contact.php
的模板文件,您将使用
get_template_part('template-parts/page-templates/page' , 'contact');
get_template_part will only work as part of a WordPress THEME.
您要做的是创建自己的 WordPress 主题(位于 wp-content/themes/ 中),然后您可以将其用于任何 PHP 模板、CSS 资源等你想要的。
您可以查看如何构建 WordPress 主题并包含所有内容in the codex here.
您需要为每个不同的设计创建一个模板文件(在 "page-templates" 目录中)。
要创建可从 de Backend(wp-admin) 访问的模板文件,您需要添加:
<?php
/*
Template Name: TEMPLATE-NAME
*/
?>
在每个文件(模板)的顶部。然后在每个文件中,您可以使用 get_template_part() 函数添加设计的块或模块。
WP_Theme
class 包含提供此过滤器的方法 get_page_templates()
:
apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
line 1103 of class-wp-theme.php
请记住,在 wordpress 中 post
和 page
是 post_type
s,
add_filter( "theme_post_templates", ...
和 add_filter( "theme_page_templates",...
应该有效。
Under the codex's "Used By" section 对于该方法,它指出:
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' );
AND/OR
add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
我知道这是一个已经有了答案的老问题,但这不是我正在寻找的解决方案。添加另一个选项,以防其他人正在寻找将所有 WordPress 模板文件移动到子文件夹的方法。在 Reddit 上找到这个 code for changing the default folder for WordPress template files 并做了一些小的调整。此代码可以放在您的主题或子主题的 functions.php 中。
/**
* Tell WordPress that we have moved default template files to the page-templates folder.
* Based on code from: https://www.reddit.com/r/Wordpress/comments/ffhjvw/moving_wordpress_theme_template_files_to/
*
* Related posts with other solutions:
*
* https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
* https://wordpress.stackexchange.com/questions/291725/store-page-template-files-in-a-subfolder
* https://wordpress.stackexchange.com/questions/312159/how-to-move-page-template-files-like-page-slug-php-to-a-sub-directory/312611#312611
*
* Template hierarchy info:
* https://developer.wordpress.org/reference/hooks/type_template_hierarchy/
* https://core.trac.wordpress.org/browser/tags/5.8.1/src/wp-includes/template.php
* https://developer.wordpress.org/themes/basics/organizing-theme-files/#page-templates-folder
*
* @param array $templates A list of candidates template files.
* @return string Full path to template file.
*/
function change_template_path($templates) {
// The custom sub-directory for page templates in your theme.
$custom_sub_dir = 'page-templates';
// Don't use the custom template directory in unexpected cases.
if(empty($templates) || ! is_array($templates)) {
return $templates;
}
$page_template_id = 0;
$count = count( $templates);
if($templates[0] === get_page_template_slug()) {
// if there is a custom template, then our page-{slug}.php template is at the next index
$page_template_id = 1;
}
// The last one in $templates is page.php, single.php, or archives.php depending on the type of template hierarchy being read.
// Paths of all items starting from $page_template_id will get updated
for($i = $page_template_id; $i < $count ; $i++) {
$templates[$i] = $custom_sub_dir . '/' . $templates[$i];
}
return $templates;
}
// Add filters to override the path for each WP template hierarchy.
// These are all the template hierarchy filters. You should probably only override the ones you need.
add_filter('404_template_hierarchy', 'change_template_path');
add_filter('archive_template_hierarchy', 'change_template_path');
add_filter('attachment_template_hierarchy', 'change_template_path');
add_filter('author_template_hierarchy', 'change_template_path');
add_filter('category_template_hierarchy', 'change_template_path');
add_filter('date_template_hierarchy', 'change_template_path');
add_filter('embed_template_hierarchy', 'change_template_path');
add_filter('frontpage_template_hierarchy', 'change_template_path');
add_filter('home_template_hierarchy', 'change_template_path');
// If you override the index hierarchy, be sure to add an index.php template in your custom template folder.
add_filter('index_template_hierarchy', 'change_template_path');
add_filter('page_template_hierarchy', 'change_template_path');
add_filter('paged_template_hierarchy', 'change_template_path');
add_filter('privacypolicy_template_hierarchy', 'change_template_path');
add_filter('search_template_hierarchy', 'change_template_path');
add_filter('single_template_hierarchy', 'change_template_path');
add_filter('singular_template_hierarchy', 'change_template_path');
add_filter('tag_template_hierarchy', 'change_template_path');
add_filter('taxonomy_template_hierarchy', 'change_template_path');
不久前我开始了一个新的 Wordpress 项目。但是我遇到了一个问题。由于有多种设计,我需要为页面创建多个模板,posts 和不同页面模板上的文本格式输出。
由于模板文件太多,我想创建一些子目录。我知道自 Wordpress 3.4 及更高版本以来,您可以将子目录名称 page-templates
用于所有页面模板,但我如何将其用于格式文件和 post 文件。
是的,我确实尝试添加如下功能:
get_template_part('/template-parts/page-templates' , 'page');
和
require( get_template_directory() . '/template-parts/template-tags.php' );
我想创建的理想目录结构如下:
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
等文件夹。这些文件夹设置正确。我的目的是,在我成功创建结构后,能够 select 像 /wp-admin
编辑页面部分中的页面模板一样的模板。
这是您需要执行的操作的示例。
- directory1 (dir)
-- directory2 (dir)
--- template-file.php
要使用 get_template_part
访问此模板,您可以这样做:
get_template_part( 'directory1/directory2/template', 'file' );
'directory1/directory2
定义目录结构。
template'
是模板名称中 -
之前的字符串。
'file'是模板名称中dash
后面的字符串。
因此在您的情况下,如果您的 page-templates
目录中有一个名为 page-contact.php
的模板文件,您将使用
get_template_part('template-parts/page-templates/page' , 'contact');
get_template_part will only work as part of a WordPress THEME.
您要做的是创建自己的 WordPress 主题(位于 wp-content/themes/ 中),然后您可以将其用于任何 PHP 模板、CSS 资源等你想要的。
您可以查看如何构建 WordPress 主题并包含所有内容in the codex here.
您需要为每个不同的设计创建一个模板文件(在 "page-templates" 目录中)。
要创建可从 de Backend(wp-admin) 访问的模板文件,您需要添加:
<?php
/*
Template Name: TEMPLATE-NAME
*/
?>
在每个文件(模板)的顶部。然后在每个文件中,您可以使用 get_template_part() 函数添加设计的块或模块。
WP_Theme
class 包含提供此过滤器的方法 get_page_templates()
:
apply_filters( "theme_{$post_type}_templates", $post_templates, $this, $post, $post_type );
line 1103 of class-wp-theme.php
请记住,在 wordpress 中 post
和 page
是 post_type
s,
add_filter( "theme_post_templates", ...
和 add_filter( "theme_page_templates",...
应该有效。
Under the codex's "Used By" section 对于该方法,它指出:
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' );
AND/OR
add_filter( 'theme_post_templates', 'deep_templates' );
add_filter( 'theme_my-custom-cpt_templates', 'deep_templates' );
我知道这是一个已经有了答案的老问题,但这不是我正在寻找的解决方案。添加另一个选项,以防其他人正在寻找将所有 WordPress 模板文件移动到子文件夹的方法。在 Reddit 上找到这个 code for changing the default folder for WordPress template files 并做了一些小的调整。此代码可以放在您的主题或子主题的 functions.php 中。
/**
* Tell WordPress that we have moved default template files to the page-templates folder.
* Based on code from: https://www.reddit.com/r/Wordpress/comments/ffhjvw/moving_wordpress_theme_template_files_to/
*
* Related posts with other solutions:
*
* https://wordpress.stackexchange.com/questions/17385/custom-post-type-templates-from-plugin-folder
* https://wordpress.stackexchange.com/questions/291725/store-page-template-files-in-a-subfolder
* https://wordpress.stackexchange.com/questions/312159/how-to-move-page-template-files-like-page-slug-php-to-a-sub-directory/312611#312611
*
* Template hierarchy info:
* https://developer.wordpress.org/reference/hooks/type_template_hierarchy/
* https://core.trac.wordpress.org/browser/tags/5.8.1/src/wp-includes/template.php
* https://developer.wordpress.org/themes/basics/organizing-theme-files/#page-templates-folder
*
* @param array $templates A list of candidates template files.
* @return string Full path to template file.
*/
function change_template_path($templates) {
// The custom sub-directory for page templates in your theme.
$custom_sub_dir = 'page-templates';
// Don't use the custom template directory in unexpected cases.
if(empty($templates) || ! is_array($templates)) {
return $templates;
}
$page_template_id = 0;
$count = count( $templates);
if($templates[0] === get_page_template_slug()) {
// if there is a custom template, then our page-{slug}.php template is at the next index
$page_template_id = 1;
}
// The last one in $templates is page.php, single.php, or archives.php depending on the type of template hierarchy being read.
// Paths of all items starting from $page_template_id will get updated
for($i = $page_template_id; $i < $count ; $i++) {
$templates[$i] = $custom_sub_dir . '/' . $templates[$i];
}
return $templates;
}
// Add filters to override the path for each WP template hierarchy.
// These are all the template hierarchy filters. You should probably only override the ones you need.
add_filter('404_template_hierarchy', 'change_template_path');
add_filter('archive_template_hierarchy', 'change_template_path');
add_filter('attachment_template_hierarchy', 'change_template_path');
add_filter('author_template_hierarchy', 'change_template_path');
add_filter('category_template_hierarchy', 'change_template_path');
add_filter('date_template_hierarchy', 'change_template_path');
add_filter('embed_template_hierarchy', 'change_template_path');
add_filter('frontpage_template_hierarchy', 'change_template_path');
add_filter('home_template_hierarchy', 'change_template_path');
// If you override the index hierarchy, be sure to add an index.php template in your custom template folder.
add_filter('index_template_hierarchy', 'change_template_path');
add_filter('page_template_hierarchy', 'change_template_path');
add_filter('paged_template_hierarchy', 'change_template_path');
add_filter('privacypolicy_template_hierarchy', 'change_template_path');
add_filter('search_template_hierarchy', 'change_template_path');
add_filter('single_template_hierarchy', 'change_template_path');
add_filter('singular_template_hierarchy', 'change_template_path');
add_filter('tag_template_hierarchy', 'change_template_path');
add_filter('taxonomy_template_hierarchy', 'change_template_path');