Wordpress Get Custom Taxonomy/Term Parents - get_term_parents()


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
  2. $chain = '';
  3. $parent = &get_term( $id, $taxonomy );
  4. if ( is_wp_error( $parent ) )
  5. return $parent;
  6.  
  7. if ( $nicename )
  8. $name = $parent->slug;
  9. else
  10. $name = $parent->name;
  11.  
  12. if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  13. $visited[] = $parent->parent;
  14. $chain .= get_term_parents( $parent->parent, $link, $separator, $nicename, $visited );
  15. }
  16.  
  17. if ( $link )
  18. $chain .= '<a href="' . get_term_link( $parent->slug, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
  19. else
  20. $chain .= $name.$separator;
  21.  
  22. return $chain;
  23. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.