[wp-trac] [WordPress Trac] #35886: get_taxonomy_parents function instead/inside get_category_parents

WordPress Trac noreply at wordpress.org
Sat Feb 20 11:34:08 UTC 2016


#35886: get_taxonomy_parents function instead/inside get_category_parents
-------------------------+-----------------------------
 Reporter:  hosein71     |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Taxonomy     |    Version:  4.4.2
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 Simply add one more arguments in {{{ get_category_parents}}} function for
 getting custom taxonomy parents.
 It can be like this:
 {{{#!php
 function get_taxonomy_parents( $id, $taxonomy = 'category', $link = false,
 $separator = '/', $nicename = false, $visited = array() ) {
         $chain = '';
         $parent = get_term( $id, $taxonomy );
         if ( is_wp_error( $parent ) )
                 return $parent;

         if ( $nicename )
                 $name = $parent->slug;
         else
                 $name = $parent->name;

         if ( $parent->parent && ( $parent->parent != $parent->term_id ) &&
 !in_array( $parent->parent, $visited ) ) {
                 $visited[] = $parent->parent;
                 $chain .= get_taxonomy_parents( $parent->parent,
 $taxonomy, $link, $separator, $nicename, $visited );
         }

         if ( $link )
                 $chain .= '<a href="' . esc_url( get_category_link(
 $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
         else
                 $chain .= $name.$separator;
         return $chain;
 }
 }}}

 So developers can use it for getting any taxonomy parents.
 The possible problem is: The given taxonomy is not Hierarchical.
 The solution is checking taxonomy by {{{is_taxonomy_hierarchical(
 $taxonomy )}}} and return false if it is not.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35886>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list