Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_term_top_most_parent($term_id, $taxonomy){
- $parent = get_term_by( 'id', $term_id, $taxonomy);
- while ($parent->parent != 0){
- $parent = get_term_by( 'id', $term_id, $taxonomy);
- }
- return $parent;
- }
- // so once you have this function you can just loop over the results returned by wp_get_object_terms
- function hey_top_parents($taxonomy) {
- global $post;
- $terms = wp_get_object_terms(post->ID, $taxonomy);
- $top_parent_terms = array();
- foreach ($terms as $term){
- //get top level parent
- $top_parent = get_term_top_most_parent($term->ID, $taxonomy);
- //check if you have it in your array to only add it once
- if (!in_array($top_parent->ID,$top_parent_terms)){
- $top_parent_terms[] = $top_parent;
- }
- }
- foreach ($top_parent_terms as $term) {
- $r = '<a href="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</a>';
- }
- // return $r;
- return print_r($top_parent_terms);
- // This shows an empty array (( [0] => )), so could the problem be with the get_term_top_most_parent() function?
- } // End of top parents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement