最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom Template for one Taxonomy Term

programmeradmin3浏览0评论

'''' Hallo , I have a custom post type called Grants , Taxonomy Called scholarship_degree and a Term Called all-degrees-scholarships. I have made a single-grants but I need now to make a Single Template just for the Term ' all-degrees-scholarships ' from this Taxonomy ' scholarship_degree '. I have tried this code but the Problem I do not want to register this Single Term Template for all other Terms . Any Help

the Code which I am using :

               function get_grants_degrees_single_template($single_template) {
                 global $post;
                  if ($post->post_type == 'grants') {
                       $terms = get_the_terms($post->ID,  'all-degrees-scholarships');
                           if($terms && !is_wp_error( $terms )) {

                             foreach($terms as $term){
                              $single_template = dirname( __FILE__ ) . '/single-'.$term->slug.'.php';
                                      }
                                  }
                               }
                               return $single_template;
                          }
                          add_filter( "single_template", "get_grants_degrees_single_template" ) ;

'''' Hallo , I have a custom post type called Grants , Taxonomy Called scholarship_degree and a Term Called all-degrees-scholarships. I have made a single-grants but I need now to make a Single Template just for the Term ' all-degrees-scholarships ' from this Taxonomy ' scholarship_degree '. I have tried this code but the Problem I do not want to register this Single Term Template for all other Terms . Any Help

the Code which I am using :

               function get_grants_degrees_single_template($single_template) {
                 global $post;
                  if ($post->post_type == 'grants') {
                       $terms = get_the_terms($post->ID,  'all-degrees-scholarships');
                           if($terms && !is_wp_error( $terms )) {

                             foreach($terms as $term){
                              $single_template = dirname( __FILE__ ) . '/single-'.$term->slug.'.php';
                                      }
                                  }
                               }
                               return $single_template;
                          }
                          add_filter( "single_template", "get_grants_degrees_single_template" ) ;

Share Improve this question asked Apr 1, 2020 at 21:52 Ahmad Al HaririAhmad Al Hariri 215 bronze badges 3
  • I'm not sure I understand is this for a term archive, or is this for posts that have a particular term? – Tom J Nowell Commented Apr 1, 2020 at 22:43
  • No not for or as an archive page , just a particular term which i want to make it a sinlge template different from other terms Template – Ahmad Al Hariri Commented Apr 2, 2020 at 11:05
  • Can you fix the indenting on the code in your question? – Tom J Nowell Commented Apr 2, 2020 at 11:36
Add a comment  | 

1 Answer 1

Reset to default 0

That's easy, instead of trying to change which template gets loaded, load the single-grants.php template, then use get_template_part inside it, and pass different values based on the terms the post has.

Be careful though, single-termname.php is not a good naming scheme, as it could be abused, e.g. you could encounter clashes. It would be better to use a naming scheme such as single-grant-termname.php

发布评论

评论列表(0)

  1. 暂无评论