Skip to content

Instantly share code, notes, and snippets.

@austinjreilly
Created February 26, 2014 16:56
Show Gist options
  • Save austinjreilly/9233570 to your computer and use it in GitHub Desktop.
Save austinjreilly/9233570 to your computer and use it in GitHub Desktop.
Generates list of most popular terms
//Adapted from http://wpsnipp.com/index.php/tags/show-most-used-tags-in-list/
function top_terms( $termName ){
$terms = get_terms( $termName );
$counts = $termLinks = array();
foreach ( $terms as $term ){
$counts[$term->name] = $term->count;
$termLinksArray[$term->name] = get_term_link( $term );
}
asort( $counts );
$counts = array_reverse( $counts, true );
$i = 0;
foreach ( $counts as $term => $count ) {
$termLink = $termLinksArray[$term];
$i++;
$termSanitized = str_replace(' ', ' ', wp_specialchars( $term ));
if( $i < 11 ){
$termList .= '<li><a href="'.$termLink.'">'.$termSanitized.' ('. $count .')</a></li>';
}
}
return $termList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment