dongyou26216708 2016-07-25 14:59
浏览 62
已采纳

按标签ID或名称为特定标签创建single.php

i have multiple tags and want to create multiple single.php for everyone tag.

How create single.php for tags??

This code working well for category, how to edit it for tags??

 function my_category_templates($single_template) {
 global $post;

 if ( in_category( 'raspee' )) {
    $single_template = dirname( __FILE__ ) . '/single-raspee.php';
 }
  return $single_template;
 }
 add_filter( "single_template", "my_category_templates" );
  • 写回答

1条回答 默认 最新

  • doutuoben6908 2016-07-25 18:40
    关注

    You can check for tag with has_tag() function...

    if ( has_tag( 'awesome' )) {
        $single_template = dirname( __FILE__ ) . '/single-awesome.php';
    }
    

    You entire code will look like this...

    function my_category_templates($single_template) {
    global $post;
    
      if ( in_category( 'raspee' )) {
        $single_template = dirname( __FILE__ ) . '/single-raspee.php';
      } else if ( has_tag( 'awesome' )) {
        $single_template = dirname( __FILE__ ) . '/single-awesome.php';
      }
      return $single_template;
    }
     add_filter( "single_template", "my_category_templates" );
    

    We added else if to test for tag it only if first condition is not met, feel free to use if again if you have to...
    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部