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');
echo
in your code was just for testing?category
terms for the post, then have you confirmed that the post did have one or more categories, and does your CPT support thecategory
taxonomy?