Advertisement
luisabarca

Untitled

Oct 16th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function custom_taxonomies_terms_links()
  2. {
  3.     // get post by post id
  4.     $post = get_post( $post->ID );
  5.  
  6.     // get post type by post
  7.     $post_type = $post->post_type;
  8.  
  9.     // get post type taxonomies
  10.     $taxonomies = get_object_taxonomies( $post_type, 'objects' );
  11.    
  12.     $out = '';
  13.    
  14.     $format_start_list = "<h2>%s</h2><ul>\n";
  15.     $format_start_parent = "<li><h3>%s</h3><ul>\n";
  16.     $format_item = "<li><a href=\"%s\">%s</a></li>\n";
  17.     $format_end_parent = "</ul></li>\n";
  18.     $format_end_list = "</ul>\n";
  19.    
  20.     foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
  21.         $ordered_terms = array();
  22.         $parent_terms = array();
  23.         $parents = 0;
  24.        
  25.         // get the terms related to post
  26.         $terms = get_the_terms( $post->ID, $taxonomy_slug );
  27.  
  28.         usort($terms, 'order_cats');
  29.        
  30.         if ( !empty( $terms ) ) {
  31.             $out .= sprintf($format_start_list, $taxonomy->label);
  32.  
  33.             foreach ( $terms as $term ) {
  34.                 if ( $term->parent < 1 ) {
  35.                     $parent_terms[$parents]['id'] = $term->term_id;
  36.                     $parent_terms[$parents++]['parent'] = $term;
  37.                 } else {
  38.                     $ordered_terms[$term->parent][$term->term_id] = $term;                    
  39.                 }
  40.             }
  41.            
  42.             foreach ($parent_terms as $id => $terms) {
  43.                 $out .= sprintf($format_start_parent, $terms['parent']->name);
  44.            
  45.                 foreach ($ordered_terms[$terms['id']] as $id2 => $term) {
  46.                     $out .= sprintf($format_item, get_term_link($term->slug, $taxonomy_slug), $term->name);
  47.                 }
  48.                
  49.                 $out .= $format_end_parent;
  50.             }
  51.            
  52.  
  53.             $out .= $format_end_list;
  54.         }
  55.     }
  56.  
  57.     return $out;
  58. }
  59.  
  60. function order_cats($a, $b)
  61. {
  62.     if ($a->term_order == $b->term_order) {
  63.         return 0;
  64.     }
  65.    
  66.     return $a->term_order < $b->term_order ? -1 : 1;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement