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;
}
Tuesday, August 21, 2018
get the primary category for a post or all category list of post
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment