admin管理员组文章数量:1026989
I have troubles with getting the posts primary set category. How should I achieve this?
My code:
global $post;
$terms = get_the_terms( $post->ID, 'event-categories');
if($terms) {
foreach( $terms as $term ) {
$cat_obj = get_term($term->term_id, 'event-categories');
$cat_slug = $cat_obj->slug;
}
}
and in my html
<div class="post_kachel <?php echo $cat_slug; ?>">
I have troubles with getting the posts primary set category. How should I achieve this?
My code:
global $post;
$terms = get_the_terms( $post->ID, 'event-categories');
if($terms) {
foreach( $terms as $term ) {
$cat_obj = get_term($term->term_id, 'event-categories');
$cat_slug = $cat_obj->slug;
}
}
and in my html
<div class="post_kachel <?php echo $cat_slug; ?>">
Share
Improve this question
asked Nov 30, 2018 at 8:37
SengelYTPISengelYTPI
151 silver badge4 bronze badges
1
- Anyone? If something is not clear, then please tell me - I'll try to explain it as best as I can. – SengelYTPI Commented Nov 30, 2018 at 12:18
1 Answer
Reset to default 2Here you go. Add this to functions.php and call it from anywhere you want.
function get_post_primary_category($post_id, $term='category', $return_all_categories=false){ $return = array(); if (class_exists('WPSEO_Primary_Term')){ // Show Primary category by Yoast if it is enabled & set $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id ); $primary_term = get_term($wpseo_primary_term->get_primary_term()); if (!is_wp_error($primary_term)){ $return['primary_category'] = $primary_term; } } if (empty($return['primary_category']) || $return_all_categories){ $categories_list = get_the_terms($post_id, $term); if (empty($return['primary_category']) && !empty($categories_list)){ $return['primary_category'] = $categories_list[0]; //get the first category } if ($return_all_categories){ $return['all_categories'] = array(); if (!empty($categories_list)){ foreach($categories_list as &$category){ $return['all_categories'][] = $category->term_id; } } } } return $return; }
I have troubles with getting the posts primary set category. How should I achieve this?
My code:
global $post;
$terms = get_the_terms( $post->ID, 'event-categories');
if($terms) {
foreach( $terms as $term ) {
$cat_obj = get_term($term->term_id, 'event-categories');
$cat_slug = $cat_obj->slug;
}
}
and in my html
<div class="post_kachel <?php echo $cat_slug; ?>">
I have troubles with getting the posts primary set category. How should I achieve this?
My code:
global $post;
$terms = get_the_terms( $post->ID, 'event-categories');
if($terms) {
foreach( $terms as $term ) {
$cat_obj = get_term($term->term_id, 'event-categories');
$cat_slug = $cat_obj->slug;
}
}
and in my html
<div class="post_kachel <?php echo $cat_slug; ?>">
Share
Improve this question
asked Nov 30, 2018 at 8:37
SengelYTPISengelYTPI
151 silver badge4 bronze badges
1
- Anyone? If something is not clear, then please tell me - I'll try to explain it as best as I can. – SengelYTPI Commented Nov 30, 2018 at 12:18
1 Answer
Reset to default 2Here you go. Add this to functions.php and call it from anywhere you want.
function get_post_primary_category($post_id, $term='category', $return_all_categories=false){ $return = array(); if (class_exists('WPSEO_Primary_Term')){ // Show Primary category by Yoast if it is enabled & set $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id ); $primary_term = get_term($wpseo_primary_term->get_primary_term()); if (!is_wp_error($primary_term)){ $return['primary_category'] = $primary_term; } } if (empty($return['primary_category']) || $return_all_categories){ $categories_list = get_the_terms($post_id, $term); if (empty($return['primary_category']) && !empty($categories_list)){ $return['primary_category'] = $categories_list[0]; //get the first category } if ($return_all_categories){ $return['all_categories'] = array(); if (!empty($categories_list)){ foreach($categories_list as &$category){ $return['all_categories'][] = $category->term_id; } } } } return $return; }
本文标签: phpGet Post Primary Category
版权声明:本文标题:php - Get Post Primary Category 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745670302a2162416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论