Posted By

noelj on 08/18/11


Tagged

php wordpress


Versions (?)

Wordpress Get Custom Taxonomy/Term Parents - get_term_parents()


 / Published in: PHP
 

  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
Posted By: mrefaie on March 27, 2013

There is a problem with your snippet @ line 14 $chain .= gettermparents( $parent->parent, $link, $separator, $nicename, $visited ); It think should be like this: $chain .= gettermparents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );

You need to login to post a comment.