php - Wordpress get category parents as category array -
if @ documentation get_category_parents() you'll see resturns string names of categories seperated seperator. now, don't know if me seems extremely stupid. i'm expecting array of actual category objects. can't have guess..
what best way parent categories in format want them?
try 1 correct result
if want parent category of post try this
$parent_cat_array = get_post_ancestors( $post ); //$post object or can pass post id
if want parent category id try one...
// determine topmost parent of term function get_term_top_most_parent($term_id, $taxonomy) { // start current term $parent = get_term_by('id', $term_id, $taxonomy); // climb hierarchy until reach term parent = '0' while ($parent->parent != '0') { $term_id = $parent->parent; $parent = get_term_by('id', $term_id, $taxonomy); } return $parent; }
function place in functions.php
call function in page, post or custom template
$term_parent = get_term_top_most_parent($term_id, $taxonomy); // in case of default category taxonomy category
here function return parent category or custom taxonomy tested!!!
Comments
Post a Comment