我有CPT“活动”。我创建了单个事件。php。
我希望一个特定事件使用不同于单个事件的模板。
我在其他地方读到,这可以通过创建一个事件(slug)来实现。php,但我试过了,它不起作用。WP使用单个事件。php。(我在WordPress文档中找不到这个,所以我想我可能误解了?)
有没有办法做到这一点?
我有CPT“活动”。我创建了单个事件。php。
我希望一个特定事件使用不同于单个事件的模板。
我在其他地方读到,这可以通过创建一个事件(slug)来实现。php,但我试过了,它不起作用。WP使用单个事件。php。(我在WordPress文档中找不到这个,所以我想我可能误解了?)
有没有办法做到这一点?
有关WordPress使用的模板,请始终参考Template hierarchy scheme 在法典中。
如你所见,single-{$posttype}-{$slug}.php
不存在,只有single-{$posttype}.php
.
要执行所需操作,请查看过滤器\'single_template\'
:
add_filter( \'single_template\', function( $template ) {
global $post;
if ( $post->post_type === \'event\' ) {
$locate_template = locate_template( "single-event-{$post->post_name}.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
在您的functions.php
, 您可以创建文件single-event-{$slug}.php
它将由WordPress加载。在函数文件中为子主题使用此代码,并将8更改为要在其上使用自定义单CPT模板的帖子ID。
function get_custom_post_type_template($single_template) {
global $post;
if ( is_single(\'8\') ) {
$single_template = get_stylesheet_directory() . \'/custom-cpt-template.php\';
}
return $single_template;
}
add_filter( \'single_template\', \'get_custom_post_type_template\' );
编辑:我仍然有这个让我抓狂的问题-有什么可以帮我的吗?谢谢我有一个自定义的博客。具有自定义但基本提要的php。代码如下: <div class=\"container\"> <div class=\"sixteen columns header\"> <div class=\"six columns alpha\">