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.
-
function wp_generate_tag_cloud( $tags, $args = ” ) {
-
global $wp_rewrite;
-
’smallest’ => 8, ‘largest’ => 22, ‘unit’ => ‘pt’, ‘number’ => 45,
-
‘format’ => ‘flat’, ‘orderby’ => ‘name’, ‘order’ => ‘ASC’
-
);
-
$args = wp_parse_args( $args, $defaults );
-
if ( !$tags )
-
return;
-
$counts_display[$tag->name] = $tag->count;
-
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
-
if ( is_wp_error( $tag_links[$tag->name] ) )
-
return $tag_links[$tag->name];
-
$tag_ids[$tag->name] = $tag->term_id;
-
}
-
if ( $spread <= 0 )
-
$spread = 1;
-
$font_spread = $largest - $smallest;
-
if ( $font_spread <= 0 )
-
$font_spread = 1;
-
$font_step = $font_spread / $spread;
-
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
-
if ( ‘name’ == $orderby )
-
else
-
if ( ‘DESC’ == $order )
-
foreach ( $counts as $tag => $count ) {
-
$tag_id = $tag_ids[$tag];
-
$tag_link = clean_url($tag_links[$tag]);
-
$a[] = "<a href=’$tag_link’ class=’tag-link-$tag_id’ title=’" . attribute_escape( sprintf( __(‘%d topics’), $counts_display[str_replace(‘ ’,‘ ‘,$tag)] ) ) . "’$rel style=’font-size: " .
-
( $smallest + ( ( $count - $min_count ) * $font_step ) )
-
. "$unit;’>$tag</a>";
-
}
-
switch ( $format ) :
-
case ‘array’ :
-
$return =& $a;
-
break;
-
case ‘list’ :
-
$return = "<ul class=’wp-tag-cloud’>\n\t<li>";
-
$return .= "</li>\n</ul>\n";
-
break;
-
default :
-
break;
-
endswitch;
-
return apply_filters( ‘wp_generate_tag_cloud’, $return, $tags, $args );
-
}
Tags: code, tags, web, web development, WordPress

