admin管理员组文章数量:1025539
I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,
function callback_for_portfolio_shortcode(){
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio',
'field' => 'id',
'terms' => $parent_cat
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
$post_categories = wp_get_post_categories( $post_id );
$cat_str = '';
//loop through the category and get a string of category
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_str .= $cat->name;
}
$html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine
$html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine
$html .= '<h4>'.post_permalink().'</h4>'; //works fine
$html .= '<h4>'.$cat_str.'</h4>'; //does not work
endwhile;
endif;
return $html;
}
$cat_str is empty. What am I missing here? Thanks.
I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,
function callback_for_portfolio_shortcode(){
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio',
'field' => 'id',
'terms' => $parent_cat
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
$post_categories = wp_get_post_categories( $post_id );
$cat_str = '';
//loop through the category and get a string of category
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_str .= $cat->name;
}
$html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine
$html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine
$html .= '<h4>'.post_permalink().'</h4>'; //works fine
$html .= '<h4>'.$cat_str.'</h4>'; //does not work
endwhile;
endif;
return $html;
}
$cat_str is empty. What am I missing here? Thanks.
Share Improve this question edited Apr 5, 2019 at 7:54 Abdullah Raihan Bhuiyan asked Apr 5, 2019 at 6:30 Abdullah Raihan BhuiyanAbdullah Raihan Bhuiyan 34 bronze badges 2 |1 Answer
Reset to default 3wp_get_post_categories()
and get_category()
are specifically for the category
taxonomy. You're using a custom taxonomy, portfolio_cat
, so you need to use get_the_terms()
to get categories.
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str = '';
foreach ( $post_categories as $category ) {
$cat_str .= $category->name;
}
If you just want a comma separated list of category links, you can use get_the_term_list()
to simplify the code:
$cat_str = get_the_term_list( $post_id, 'portfolio_cat', '', ', ', '' );
Or, if you don't want links, you can do this instead:
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str = implode( ', ', wp_list_pluck( $categories, 'name' ) );
Also, instead of $query->post->post_title
you should really just be using get_the_title();
.
I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,
function callback_for_portfolio_shortcode(){
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio',
'field' => 'id',
'terms' => $parent_cat
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
$post_categories = wp_get_post_categories( $post_id );
$cat_str = '';
//loop through the category and get a string of category
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_str .= $cat->name;
}
$html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine
$html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine
$html .= '<h4>'.post_permalink().'</h4>'; //works fine
$html .= '<h4>'.$cat_str.'</h4>'; //does not work
endwhile;
endif;
return $html;
}
$cat_str is empty. What am I missing here? Thanks.
I am using a CPT (post_type => 'portfolio_type', taxonomy => 'portfolio_cat'). Under this CPT, I have design, web development category. I am trying to get the post with the category. My best try,
function callback_for_portfolio_shortcode(){
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio',
'field' => 'id',
'terms' => $parent_cat
)
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
$post_categories = wp_get_post_categories( $post_id );
$cat_str = '';
//loop through the category and get a string of category
foreach($post_categories as $c){
$cat = get_category( $c );
$cat_str .= $cat->name;
}
$html .= '<h4>'.$query->post->post_title.'</h4>'; //works fine
$html .= '<h4>'.substr(get_field('job_description', $id), 0, 100).'</h4>'; //works fine
$html .= '<h4>'.post_permalink().'</h4>'; //works fine
$html .= '<h4>'.$cat_str.'</h4>'; //does not work
endwhile;
endif;
return $html;
}
$cat_str is empty. What am I missing here? Thanks.
Share Improve this question edited Apr 5, 2019 at 7:54 Abdullah Raihan Bhuiyan asked Apr 5, 2019 at 6:30 Abdullah Raihan BhuiyanAbdullah Raihan Bhuiyan 34 bronze badges 2-
You use the wrong operator to concatenate strings, you should replace it with
.=
.$cat_str .= $cat->name;
– nmr Commented Apr 5, 2019 at 7:04 - just a typo mistake. sorry! – Abdullah Raihan Bhuiyan Commented Apr 5, 2019 at 7:54
1 Answer
Reset to default 3wp_get_post_categories()
and get_category()
are specifically for the category
taxonomy. You're using a custom taxonomy, portfolio_cat
, so you need to use get_the_terms()
to get categories.
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str = '';
foreach ( $post_categories as $category ) {
$cat_str .= $category->name;
}
If you just want a comma separated list of category links, you can use get_the_term_list()
to simplify the code:
$cat_str = get_the_term_list( $post_id, 'portfolio_cat', '', ', ', '' );
Or, if you don't want links, you can do this instead:
$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str = implode( ', ', wp_list_pluck( $categories, 'name' ) );
Also, instead of $query->post->post_title
you should really just be using get_the_title();
.
本文标签: shortcodeHow to retrieve category of a post in havepost loop
版权声明:本文标题:shortcode - How to retrieve category of a post in have_post loop? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745625702a2159846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.=
.$cat_str .= $cat->name;
– nmr Commented Apr 5, 2019 at 7:04