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.

function blogView( $atts ){
  global $post;

    $blog_post = get_posts(array(
      'showposts' => 80, //add -1 if you want to show all posts
      'post_type' => 'blog'
    $blog_post = get_posts(array(
      'showposts' => 10, //add -1 if you want to show all posts
      'post_type' => 'blog',
      'tax_query' => 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*/
          foreach($category_detail as $cd){
          echo $cd->cat_name;
        /*end here*/



    return $data; 

add_shortcode( 'blog', 'blogView');
  • 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
    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
    Feb 22, 2022 at 10:15
  • 1
    @SallyCJ, Echo is for testing purposes. Yes, I have a post with a category. Feb 22, 2022 at 10:45

1 Answer 1


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.


  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.
  • Give me sometime. Let me check Feb 22, 2022 at 10:45
  • So have you done checking? Did my answer help?
    – Sally CJ
    Feb 23, 2022 at 10:24
  • 1
    Yes, I have tested the first option and I am getting the correct output. Can you explain the Additionally code? Why you have used then $atts ? Feb 23, 2022 at 10:36
  • 1
    Thank you for the help. Sure, I will check the documentation Feb 23, 2022 at 12:31
  • 1
    Thank you so much for the help. Feb 24, 2022 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.