除了按着约定指定模版文件外,还可以在 filter 中按着条件来指定。
Template Related Filters
template_include
用于更改加载的模板文件。
add_filter( 'template_include', 'my_custom_template' );
function my_custom_template( $template ) {
if ( is_single() ) {
$new_template = locate_template( array( 'single-custom.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
template_redirect
在 WordPress 确定要加载哪个模板之前执行重定向。
add_action( 'template_redirect', 'my_custom_redirect' );
function my_custom_redirect() {
if ( is_page( 'about' ) ) {
wp_redirect( home_url( '/new-about/' ) );
exit();
}
}
single_template
用于更改单篇文章的模板文件。
add_filter( 'single_template', 'my_single_template' );
function my_single_template( $single ) {
global $post;
if ( $post->post_type == 'book' ) {
return dirname( __FILE__ ) . '/single-book.php';
}
return $single;
}
page_template
用于更改页面的模板文件。
add_filter( 'page_template', 'my_page_template' );
function my_page_template( $page_template ) {
if ( is_page( 'contact' ) ) {
$page_template = dirname( __FILE__ ) . '/page-contact.php';
}
return $page_template;
}
category_template
用于更改分类存档的模板文件。
add_filter( 'category_template', 'my_category_template' );
function my_category_template( $category_template ) {
if ( is_category( 'news' ) ) {
$category_template = dirname( __FILE__ ) . '/category-news.php';
}
return $category_template;
}
tag_template
用于更改标签存档的模板文件。
add_filter( 'tag_template', 'my_tag_template' );
function my_tag_template( $tag_template ) {
if ( is_tag( 'special' ) ) {
$tag_template = dirname( __FILE__ ) . '/tag-special.php';
}
return $tag_template;
}
taxonomy_template
用于更改自定义分类法存档的模板文件。
add_filter( 'taxonomy_template', 'my_taxonomy_template' );
function my_taxonomy_template( $taxonomy_template ) {
if ( is_tax( 'genre' ) ) {
$taxonomy_template = dirname( __FILE__ ) . '/taxonomy-genre.php';
}
return $taxonomy_template;
}
author_template
用于更改作者存档的模板文件。
add_filter( 'author_template', 'my_author_template' );
function my_author_template( $author_template ) {
$author = get_queried_object();
if ( $author->user_nicename == 'john' ) {
$author_template = dirname( __FILE__ ) . '/author-john.php';
}
return $author_template;
}
date_template
用于更改日期存档的模板文件。
add_filter( 'date_template', 'my_date_template' );
function my_date_template( $date_template ) {
if ( is_date() ) {
$date_template = dirname( __FILE__ ) . '/date-custom.php';
}
return $date_template;
}
archive_template
用于更改存档页面的模板文件。
add_filter( 'archive_template', 'my_archive_template' );
function my_archive_template( $archive_template ) {
if ( is_post_type_archive( 'movie' ) ) {
$archive_template = dirname( __FILE__ ) . '/archive-movie.php';
}
return $archive_template;
}
search_template
用于更改搜索结果页面的模板文件。
add_filter( 'search_template', 'my_search_template' );
function my_search_template( $search_template ) {
if ( is_search() ) {
$search_template = dirname( __FILE__ ) . '/search-custom.php';
}
return $search_template;
}
home_template
用于更改主页的模板文件。
add_filter( 'home_template', 'my_home_template' );
function my_home_template( $home_template ) {
if ( is_home() ) {
$home_template = dirname( __FILE__ ) . '/home-custom.php';
}
return $home_template;
}
front_page_template
用于更改前端页面的模板文件。
add_filter( 'front_page_template', 'my_front_page_template' );
function my_front_page_template( $front_page_template ) {
if ( is_front_page() ) {
$front_page_template = dirname( __FILE__ ) . '/front-page-custom.php';
}
return $front_page_template;
}
404_template
用于更改 404 错误页面的模板文件。
add_filter( '404_template', 'my_404_template' );
function my_404_template( $error_template ) {
if ( is_404() ) {
$error_template = dirname( __FILE__ ) . '/404-custom.php';
}
return $error_template;
}
comments_template
用于更改评论模板文件。
add_filter( 'comments_template', 'my_comments_template' );
function my_comments_template( $comment_template ) {
if ( is_singular() ) {
$comment_template = dirname( __FILE__ ) . '/comments-custom.php';
}
return $comment_template;
}
embed_template
用于更改嵌入内容的模板文件。
add_filter( 'embed_template', 'my_embed_template' );
function my_embed_template( $embed_template ) {
if ( is_embed() ) {
$embed_template = dirname( __FILE__ ) . '/embed-custom.php';
}
return $embed_template;
}
template_directory
用于更改模板目录路径。
add_filter( 'template_directory', 'my_template_directory' );
function my_template_directory( $template_dir ) {
return $template_dir . '/custom';
}
stylesheet_directory
用于更改样式表目录路径。
add_filter( 'stylesheet_directory', 'my_stylesheet_directory' );
function my_stylesheet_directory( $stylesheet_dir ) {
return $stylesheet_dir . '/custom';
}
theme_root
用于更改主题根目录路径。
add_filter( 'theme_root', 'my_theme_root' );
function my_theme_root( $theme_root ) {
return $theme_root . '/custom';
}
theme_root_uri
用于更改主题根目录 URI。
add_filter( 'theme_root_uri', 'my_theme_root_uri' );
function my_theme_root_uri( $theme_root_uri ) {
return $theme_root_uri . '/custom';
}
home_template 和 front_page_template 有什么区别?
home_template
和 front_page_template
是 WordPress 中用于不同场景的两个模板过滤器。它们的主要区别如下:
home_template
- 用途:用于显示博客主页(即最新文章列表)。
- 触发条件:当你在 WordPress 设置中选择“显示最新文章”作为主页时,
home_template
会被调用。 -
示例:
add_filter( 'home_template', 'my_home_template' ); function my_home_template( $home_template ) { return dirname( __FILE__ ) . '/home-custom.php'; }
front_page_template
- 用途:用于显示网站的前端页面(即静态首页)。
- 触发条件:当你在 WordPress 设置中选择“显示静态页面”作为主页时,
front_page_template
会被调用。 -
示例:
add_filter( 'front_page_template', 'my_front_page_template' ); function my_front_page_template( $front_page_template ) { return dirname( __FILE__ ) . '/front-page-custom.php'; }
区别总结
home_template
:用于显示最新文章列表的页面,通常用于博客主页。front_page_template
:用于显示静态首页,通常用于自定义的前端页面。
例如,如果你的网站首页设置为显示最新文章,WordPress 会使用 home_template
。如果你的网站首页设置为显示一个静态页面,WordPress 会使用 front_page_template
。
当 filter template 中的过滤条件不满足需求时,还是会使用约定模版渲染页面?
是的,当过滤器中的条件不满足时,WordPress 会回退到使用约定的模板文件来渲染页面。这是 WordPress 模板层级系统的一部分,确保即使没有自定义过滤器或模板文件,页面也能正确显示。
示例说明
假设你有以下 single_template
过滤器:
add_filter( 'single_template', 'my_single_template' );
function my_single_template( $single ) {
global $post;
if ( $post->post_type == 'book' ) {
return dirname( __FILE__ ) . '/single-book.php';
}
return $single;
}
在这个示例中,如果文章类型是 book
,WordPress 会使用 single-book.php
模板文件。如果文章类型不是 book
,WordPress 会回退到使用默认的 single.php
模板文件。
模板层级系统
WordPress 模板层级系统定义了在不同情况下使用的模板文件的优先级。例如,对于单篇文章,模板层级可能如下:
single-{post-type}.php
(例如single-book.php
)single.php
index.php
如果没有找到特定的模板文件,WordPress 会继续向下查找,直到找到一个可用的模板文件。例如,如果没有 single-book.php
和 single.php
,WordPress 会使用 index.php
。
另一个示例
假设你有以下 category_template
过滤器:
add_filter( 'category_template', 'my_category_template' );
function my_category_template( $category_template ) {
if ( is_category( 'news' ) ) {
return dirname( __FILE__ ) . '/category-news.php';
}
return $category_template;
}
在这个示例中,如果当前分类是 news
,WordPress 会使用 category-news.php
模板文件。如果不是 news
分类,WordPress 会回退到使用默认的 category.php
模板文件。如果 category.php
不存在,WordPress 会继续查找 archive.php
,最后是 index.php
。
这种模板层级系统确保了即使没有自定义模板文件,WordPress 也能找到合适的模板文件来渲染页面。
按约定命名 theme 中的模版文件
在 WordPress 主题中,模板文件的命名遵循一定的约定,以确保 WordPress 能正确识别和加载相应的模板文件。以下是一些常见的模板文件及其命名方式:
常见模板文件命名约定
-
首页模板:
front-page.php
:用于显示静态首页(当设置为显示静态页面时)。home.php
:用于显示博客主页(当设置为显示最新文章时)。
-
单篇文章模板:
single.php
:用于显示单篇文章。single-{post-type}.php
:用于显示特定文章类型的单篇文章,例如single-book.php
。
-
页面模板:
page.php
:用于显示单个页面。page-{slug}.php
:用于显示特定页面,例如page-contact.php
。
-
分类和标签模板:
category.php
:用于显示分类存档。category-{slug}.php
:用于显示特定分类,例如category-news.php
。tag.php
:用于显示标签存档。tag-{slug}.php
:用于显示特定标签,例如tag-special.php
。
-
自定义分类法模板:
taxonomy.php
:用于显示自定义分类法存档。taxonomy-{taxonomy}.php
:用于显示特定自定义分类法,例如taxonomy-genre.php
。
-
作者模板:
author.php
:用于显示作者存档。author-{nicename}.php
:用于显示特定作者,例如author-john.php
。
-
日期模板:
date.php
:用于显示日期存档。
-
存档模板:
archive.php
:用于显示存档页面。archive-{post-type}.php
:用于显示特定文章类型的存档,例如archive-movie.php
。
-
搜索结果模板:
search.php
:用于显示搜索结果页面。
-
404 错误页面模板:
404.php
:用于显示 404 错误页面。
-
评论模板:
comments.php
:用于显示评论部分。
-
嵌入模板:
embed.php
:用于显示嵌入内容。embed-{post-type}.php
:用于显示特定文章类型的嵌入内容,例如embed-video.php
。
这些命名约定帮助 WordPress 根据不同的请求加载相应的模板文件,确保网站的正确显示。