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

按标签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" );

我有多个标签,想为每个人的标签创建多个single.php。 p>

如何为标签创建single.php? strong> p>

此代码适用于类别,如何编辑标签? p> blockquote>

  function my_category_templates($ single_template){
 global $ post; 
 
 if(in_category('raspee')){
 $  single_template = dirname(__ FILE__)。  '/single-raspee.php'; 
} 
返回$ single_template; 
} 
 add_filter(“single_template”,“my_category_templates”); 
  code>  pre> 
  div>

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.