我想知道是否有人知道如何为多个自定义帖子类型设置单个模板。例如 - 我不想设置多个完全相同的模板。
我在搜索时发现了以下代码段,但似乎无法正常工作。我在functions.php
中将其放在我正在使用的主题中。
add_filter( 'single_template', function( $template ) {
$cpt = [ 'available-properties', 'leased-sold', 'norway' ];
return in_array( get_queried_object()->post_type, $cpt, true )
? 'path/to/country-single.php'
: $template;
} );
答案 0 :(得分:0)
这似乎很棒!
add_filter( 'template_include', function( $template )
{
// your custom post types
$my_types = array( 'available-properties', 'leased-sold' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $my_types ) )
return $template;
return get_stylesheet_directory() . '/page-content__projects-single.php';
});