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; }
最新文章
- 2014年IT五大发展趋势
- 消息称PC供应商面临来自Windows 8的挑战
- amazon web services - Download large file from AWS S3 with Go SDK v2 - Stack Overflow
- performance testing - How to retrieve the dynamic value from the JMeter - Stack Overflow
- swift - How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow
- c - Interrupts on STM32F407G discovery Micro controller with HAL library to blink led - Stack Overflow
- r - Elegant vectorization of nested for loop - Stack Overflow
- Django is not updating on MacOS - Stack Overflow
- Windows Powershell: check if a symlink is "broken" - Stack Overflow
- c# - After debugging, Visual Studio 2022 holding on to .NET Core program's .dll and I can't complete next build
- python - Unet isn't working orand i'm not using it correctly - Stack Overflow
- javascript - onPointerOver and onPointerOut detect all child elements - Stack Overflow
- angular - How to resolve vitest errors; stylesheet imports and dynamically fetched modules - Stack Overflow
- python 3.x - unable to dynamically access ".variable" attribute of a ttk.RadioButton (python3) - Stack Overflo
- python - Using OpenCV to achieve a top-down view of an image with ArUco Markers - Stack Overflow
- c++ - Pointer of an object which has static storage as template non-type parameter, Clang and GCC agrees, MSVC doesn't -
- amazon web services - Nuxt build fails on AWS Amplify: Failed to find the deploy-manifest.json file in the build output - Stack