如何在Wordpress中为特定帖子类别创建自定义帖子模板?
如何为特定的帖子类别创建自定义帖子模板?
2 个回复
SO网友:Vasu Chawla
这是类别的默认模板优先级结构:
类别slug。phpcategory-ID.phpcategory。phparchive。phpindex。php
所以,如果您的类别slug是“car”,您可以添加名为category car的文件。php
如果文件无效,请确保在添加文件后重新保存永久链接:)
SO网友:Milo
我将添加此答案,因为不清楚您是询问类别存档模板还是单个自定义帖子模板。
要更改单个帖子模板,请使用single_template
过滤并检查职位是否属于特定类别。有许多Conditional Tags 可以使用确定正在查看的内容以及对象指定的术语。
在本例中,我们检查它是否是类型为foo
其中包含类别bar
已分配。
function add_posttype_slug_template( $single_template ){
if( is_singular( \'foo\' ) && in_category( \'bar\' ) ){
$single_template = locate_template( \'my-custom-template.php\' );
}
return $single_template;
}
add_filter( \'single_template\', \'add_posttype_slug_template\' );