Posted By

daipratt on 09/19/10


Tagged

wordpress tags


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

athanassiadis


Wordpress - show top 10 tags by usage


 / Published in: PHP
 

URL: http://daipratt.co.uk/tag/wordpress/

Show the top tags by usage.

  1. <?php
  2. function top_tags() {
  3. $tags = get_tags();
  4.  
  5. if (empty($tags))
  6. return;
  7.  
  8. $counts = $tag_links = array();
  9. foreach ( (array) $tags as $tag ) {
  10. $counts[$tag->name] = $tag->count;
  11. $tag_links[$tag->name] = get_tag_link( $tag->term_id );
  12. }
  13.  
  14. asort($counts);
  15. $counts = array_reverse( $counts, true );
  16.  
  17. $i = 0;
  18. foreach ( $counts as $tag => $count ) {
  19. $i++;
  20. $tag_link = clean_url($tag_links[$tag]);
  21. $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
  22. if($i < 11){
  23. print "<li><a href=\"$tag_link\">$tag ($count)</a></li>";
  24. }
  25. }
  26. }
  27. ?>
  28. <h4>Top tags</h4>
  29. <ul>
  30. <?php
  31. top_tags();
  32. ?>
  33. </ul>

Report this snippet  

You need to login to post a comment.