dragunoff

wpquestions.com, ID = 3159

Oct 17th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. function custom_list_terms( $taxonomies = array( 'category' ) , $args = array() ) {
  2.  
  3.     // vars
  4.     $r = '';
  5.     $letter = '';
  6.    
  7.     // get all terms
  8.     $terms = get_terms( $taxonomies, $args );
  9.  
  10.     // if there are any temrs
  11.     if ( $terms ) {
  12.    
  13.         // begin building output
  14.         $r .= '<h2>' . $letter . '</h2>';
  15.         $r .= '<ul class="term-archive letter-' . $letter. '">';
  16.        
  17.         // set the topmost letter to the first letter of the first term
  18.         // $letter = mb_strtoupper( mb_substr( $terms[0]->name, 0, 1 ) ); // use this when working with non-ASCII characters
  19.         $letter = strtoupper( substr( $terms[0]->name, 0, 1 ) );
  20.        
  21.         // loop terms
  22.         foreach ($terms as $term) {
  23.        
  24.             // re-assign letter variable
  25.             $old_letter = $letter;
  26.  
  27.             // get first letter of term name
  28.             // $letter = mb_strtoupper( mb_substr( $term->name, 0, 1 ) ); // use this when working with non-ASCII characters
  29.             $letter = strtoupper( substr( $term->name, 0, 1 ) );
  30.  
  31.             // check wether we're at a new letter
  32.             if( $letter != $old_letter ) {
  33.            
  34.                 // start a new section for new letter
  35.                 $r .= '</ul>';
  36.                 $r .= '<h2>' . $letter . '</h2>';
  37.                 $r .= '<ul class="term-archive letter-' . $letter . '">';
  38.                
  39.             }
  40.  
  41.             // sets term meta (to an empty string if function doesn't exist)
  42.             $term_meta = function_exists( 'get_terms_meta' ) ? get_terms_meta( $term->term_id, 'mysummary', true ) : '';
  43.                
  44.             // HTML for each term
  45.             $r .= '<li class="' . $term->taxonomy . '">';
  46.             $r .= '<a href="/statistics/term/' . $term->slug . '" title="' . sprintf( esc_attr__( 'View all post filed under %s' ), $term->name ) . '">';
  47.             $r .= $term->name;
  48.             $r .= '</a> ';
  49.             $r .= '(' . $term->count . ')';
  50.             $r .= $term_meta; // the term meta can be appended to the HTML like this
  51.             $r .= '</li>';
  52.            
  53.         }
  54.        
  55.         $r .= '</ul>';
  56.        
  57.         // echo/return the result
  58.         echo $r;
  59.        
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment