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

plugin development - Custom post category name showing empty

programmeradmin0浏览0评论

I have a custom post type and category. I have the below function on my page. I am getting the post but I am not getting the category name on the page.

I am getting the empty array

array(0) { } 

Below is the function code.

<?php 
function blogView( $atts ){
  global $post;

  if($atts['cat']=='All'){      
    $blog_post = get_posts(array(
      'showposts' => 80, //add -1 if you want to show all posts
      'post_type' => 'blog'
      ));
  }else{  
    $blog_post = get_posts(array(
      'showposts' => 10, //add -1 if you want to show all posts
      'post_type' => 'blog',
      'tax_query' => array(
        array(
      'taxonomy' => 'blogs_cats',
      'field' => 'slug',
      'terms' => $atts['cat'] //pass your term name here
        )
      ))
       );
  }
     
$data='<ul class="post-grid-list">';
    foreach($blog_post as $t){
    $tid = $t->ID;

         /*Display category code here*/
          $category_detail=get_the_category('4');//$post->ID
          foreach($category_detail as $cd){
          echo $cd->cat_name;
          }
        /*end here*/


         $data.='';

    }
    $data.='</ul>';

    return $data; 

}
add_shortcode( 'blog', 'blogView');

I have a custom post type and category. I have the below function on my page. I am getting the post but I am not getting the category name on the page.

I am getting the empty array

array(0) { } 

Below is the function code.

<?php 
function blogView( $atts ){
  global $post;

  if($atts['cat']=='All'){      
    $blog_post = get_posts(array(
      'showposts' => 80, //add -1 if you want to show all posts
      'post_type' => 'blog'
      ));
  }else{  
    $blog_post = get_posts(array(
      'showposts' => 10, //add -1 if you want to show all posts
      'post_type' => 'blog',
      'tax_query' => array(
        array(
      'taxonomy' => 'blogs_cats',
      'field' => 'slug',
      'terms' => $atts['cat'] //pass your term name here
        )
      ))
       );
  }
     
$data='<ul class="post-grid-list">';
    foreach($blog_post as $t){
    $tid = $t->ID;

         /*Display category code here*/
          $category_detail=get_the_category('4');//$post->ID
          foreach($category_detail as $cd){
          echo $cd->cat_name;
          }
        /*end here*/


         $data.='';

    }
    $data.='</ul>';

    return $data; 

}
add_shortcode( 'blog', 'blogView');
Share Improve this question asked Feb 22, 2022 at 8:18 Naren VermaNaren Verma 2491 gold badge6 silver badges19 bronze badges 3
  • Shortcodes should always return the output and not echo anything - because for example it would cause the block editor to fail in saving a post due to invalid JSON/REST API response - but I guessed the echo in your code was just for testing? – Sally CJ Commented Feb 22, 2022 at 10:10
  • Also, if you did want to retrieve the category terms for the post, then have you confirmed that the post did have one or more categories, and does your CPT support the category taxonomy? – Sally CJ Commented Feb 22, 2022 at 10:15
  • 1 @SallyCJ, Echo is for testing purposes. Yes, I have a post with a category. – Naren Verma Commented Feb 22, 2022 at 10:45
Add a comment  | 

1 Answer 1

Reset to default 0

Note that the get_the_category() documentation stated that:

Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms().

So if I guessed it correctly that you're trying to display the blogs_cats terms for the current post, then you should use get_the_terms() instead.

There's also get_the_term_list() if you just want to display a list of terms like Foo Bar, Term 2 where each term links to their own archive page.

Examples:

  1. Manually loop through the terms returned by get_the_terms() to output a simple list of term names (only):

    $category_detail = get_the_terms( $tid, 'blogs_cats' );
    
    $cats = array();
    if ( is_array( $category_detail ) ) {
        foreach ( $category_detail as $cd ) {
            $cats[] = $cd->name;
        }
    }
    
    $data .= "<li>blogs_cats for $tid: " . implode( ', ', $cats ) . '</li>';
    
  2. Or use get_the_term_list() to output a list of term names + links:

    $cat_list = get_the_term_list( $tid, 'blogs_cats', '<i>', ', ', '</i>' );
    $data .= "<li>blogs_cats for $tid: $cat_list</li>";
    

Additionally, you should call shortcode_atts() to ensure that the cat argument exists in $atts (e.g. when using just [blog], i.e. without specifying any parameters). E.g.

function blogView( $atts ){
    global $post;

    $atts = shortcode_atts( array(
        'cat' => 'All',
    ), $atts );

    // ... your code.
}
发布评论

评论列表(0)

  1. 暂无评论