php - Get Post Primary Category
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; }
最新文章
- 传统杀毒软件市场受冲击 McAfee计划裁员7100人
- Mac电脑与PC九大区别
- 分析:Windows 8平板电脑称雄市场需三大因素
- How can I design a button with two colors and a curved border in CSS? - Stack Overflow
- docker - how to fix "communications link failure the last packet sent successfully to the server was 0 milliseconds ago
- html - Audio tag working in Chrome but not Safari? - Stack Overflow
- python - How to solve the problem of multi-layer contours obtained by using Zernick moments for sub-pixel edge detection? - Stac
- tracking - Add custom hand gestures in three.js - Stack Overflow
- javascript - How to create perfect hash with ASCII symbols as input, where output hash is always the same for each ASCII sequenc
- javascript - How to retarget 3D pose landmarks (points in 3d space) onto a rigged humanoid model in Three.js? - Stack Overflow
- reactjs - why react dev tools paint highlights to components in this example - Stack Overflow
- java - Handling order Id In OMS system on application level - Stack Overflow
- javascript - Why does every object in BabylonJS require a name? - Stack Overflow
- ruby - How to render HTML with Haml::Engine from Haml 6.3? - Stack Overflow
- GitHub disallow merge and rebase commits for multiple repositories - Stack Overflow
- python - ReCaptcha v3 works locally but not in deployment - Stack Overflow
- Spring Boot Application in Docker Container Shuts Down Immediately After Startup - Stack Overflow