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
|
1 Answer
Reset to default 0Note 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:
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>';
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.
}
echo
in your code was just for testing? – Sally CJ Commented Feb 22, 2022 at 10:10category
terms for the post, then have you confirmed that the post did have one or more categories, and does your CPT support thecategory
taxonomy? – Sally CJ Commented Feb 22, 2022 at 10:15