dongyou26216708 2016-07-25 22: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-26 02: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.

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

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图