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 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python