Wordpress - 为多个自定义帖子类型设置自定义模板

时间:2017-09-20 20:05:00

标签: wordpress wordpress-theming custom-post-type

我想知道是否有人知道如何为多个自定义帖子类型设置单个模板。例如 - 我不想设置多个完全相同的模板。

代码

我在搜索时发现了以下代码段,但似乎无法正常工作。我在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;

} );

1 个答案:

答案 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';
 });
相关问题