Subscribe to RSS Subscribe to Comments

commafruit.co.uk. welcome.

Site upgrade, Wordpress 2.3.1, Tags

So, I’ve finally upgraded this site to use a version 2.x series Wordpress. This version (2.3.1) includes tagging support built in, which was a big plus for me as I’d be looking at installing something like UTW, but putting it off because I knew I planned to upgrade. Installation/upgrade of Wordpress is impressively straightforward by the way.

Anyway, after going through and tagging most of my entries, it was clear that I had a small number of tags with like 20 posts, and then lots of others with 1-5, which meant I had a list of words with a couple of them highlighted, but unable to see the difference between a tag with 1 post and one with 4. Luckily, I know PHP. :)

The code below replaces the wp_generate_tag_cloud function in the wp-includes/category-template.php file. Essentially, rather than taking the default distribution of number of posts for a tag then calculating the size, it takes the natural log (aka ln or loge), which results in a more even distribution, and so more similarly sized tags!

This code was marked up using the wonderful GeSHi.

  1. function wp_generate_tag_cloud( $tags, $args = ) {
  2.         global $wp_rewrite;
  3.         $defaults = array(
  4.                 ’smallest’ => 8, ‘largest’ => 22, ‘unit’ => ‘pt’, ‘number’ => 45,
  5.                 ‘format’ => ‘flat’, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’
  6.         );
  7.         $args = wp_parse_args( $args, $defaults );
  8.         extract($args);
  9.         if ( !$tags )
  10.                 return;
  11.         $counts = $tag_links = $counts_display = array();
  12.         foreach ( (array) $tags as $tag ) {
  13.                 $counts[$tag->name] = log($tag->count) + 1;
  14.                 $counts_display[$tag->name] = $tag->count;
  15.                 $tag_links[$tag->name] = get_tag_link( $tag->term_id );
  16.                 if ( is_wp_error( $tag_links[$tag->name] ) )
  17.                         return $tag_links[$tag->name];
  18.                 $tag_ids[$tag->name] = $tag->term_id;
  19.         }
  20.         $min_count = min($counts);
  21.         $spread = max($counts) - $min_count;
  22.         if ( $spread <= 0 )
  23.                 $spread = 1;
  24.         $font_spread = $largest - $smallest;
  25.         if ( $font_spread <= 0 )
  26.                 $font_spread = 1;
  27.         $font_step = $font_spread / $spread;
  28.         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  29.         if ( ‘name’ == $orderby )
  30.                 uksort($counts, ’strnatcasecmp’);
  31.         else
  32.                 asort($counts);
  33.         if ( ‘DESC’ == $order )
  34.                 $counts = array_reverse( $counts, true );
  35.         $a = array();
  36.         $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ‘ rel="tag"’ : ;
  37.         foreach ( $counts as $tag => $count ) {
  38.                 $tag_id = $tag_ids[$tag];
  39.                 $tag_link = clean_url($tag_links[$tag]);
  40.                 $tag = str_replace(‘ ‘, ‘&nbsp;’, wp_specialchars( $tag ));
  41.                 $a[] = "<a href=’$tag_link’ class=’tag-link-$tag_id’ title=’" . attribute_escape( sprintf( __(‘%d topics’), $counts_display[str_replace(‘&nbsp;’,‘ ‘,$tag)] ) ) . "’$rel style=’font-size: " .
  42.                         ( $smallest + ( ( $count - $min_count ) * $font_step ) )
  43.                         . "$unit;’>$tag</a>";
  44.         }
  45.         switch ( $format ) :
  46.         case ‘array’ :
  47.                 $return =& $a;
  48.                 break;
  49.         case ‘list’ :
  50.                 $return = "<ul class=’wp-tag-cloud’>\n\t<li>";
  51.                 $return .= join("</li>\n\t<li>", $a);
  52.                 $return .= "</li>\n</ul>\n";
  53.                 break;
  54.         default :
  55.                 $return = join("\n", $a);
  56.                 break;
  57.         endswitch;
  58.         return apply_filters( ‘wp_generate_tag_cloud’, $return, $tags, $args );
  59. }
Tags: , , , ,
AddThis Social Bookmark Button

Based on FluidityTheme Redesigned by Kaushal Sheth | All content by Ben Parsons unless otherwise stated | Askimet has filtered: 199 spam messages.