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');
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. Commented Feb 22, 2022 at 10:45

1 Answer 1

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.
}
8
  • Give me sometime. Let me check Commented Feb 22, 2022 at 10:45
  • So have you done checking? Did my answer help?
    – Sally CJ
    Commented 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 ? Commented Feb 23, 2022 at 10:36
  • 1
    Thank you for the help. Sure, I will check the documentation Commented Feb 23, 2022 at 12:31
  • 1
    Thank you so much for the help. Commented 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.