Hit or Miss

Weighted Categories

Following the example of Flickr’s weighted list of photo tags (a bigger font size denotes more items), here’s a plug-in for WordPress that creates a weighted list of category links.

Check out my Archive Page for a sample.

12-20-2004 5:40pm Update 1.1: I changed the code a bit to allow for Categories whose slugs are different than their names (ex. “movable_type” vs. “Movable Type”).

12-22-2004 12:00pm Update 1.2: I changed the code yet again to fix a problem with division by 0 errors.

  1. First, save this file as weighted_categories.php in your plug-ins directory.

  2. Activate the plug-in from Plugins Admin panel.

  3. In the file where you want to display the weighted list of categories (ex. archives.php), insert the following code:
    < ? $minfont = 10; $maxfont = 40; $fontunit = "pt"; $category_ids_to_exclude = "1,25"; weighted_categories($minfont, $maxfont, $fontunit, $category_ids_to_exclude); ?>
  4. Adjust the minimum and maximum font sizes of the weighted category list with $minfont and $maxfont. You can specify the measuring units of the fonts (ex. pt, px, em, etc) with $fontunit. If there are any categories you want to exclude from the list, put the category IDs in $category_ids_to_exclude.

  5. I added the following code to my CSS definitions to style the weighted list of categories:
    #categories { border: 1px solid #ccc; background-color: #eee;
    	padding: 10px; width: 500px; text-align: justify; }
    #categories a { text-decoration: none; }

→ 67 Comments

67 responses so far (Respond)

Gravatar

Hello.
I don’t want to use the categories-list, I want to show the list by keywords like you have on your site. Is it something that can be a simply changed in the code I insert in the page.
Or maybe is going to be a another plugin? Let me know! I must apologize for my bad english, have a nice day.

//Örjan

(thinking of this code)

Oerjan | 18 Feb 2005
Gravatar

Same for me, same for me… I want keywords !! ;o)

elaine | 27 Feb 2005
Gravatar

gracias por este plugin :-)

lo instalé y funciona a la perfección

ylek | 10 Mar 2005
Gravatar

Hello, it wanted to know if this plug ins runs under wp 1,2, because I made the installation, but does not show anything, the page is in target.

Andrés | 11 Mar 2005
Gravatar

there is a bug in that script

the link for the latest categoriy listet is not correct :|

eAk | 12 Mar 2005
Gravatar

Might be a bug in wordpress, I don’t see anything in the plugin to explain this. I’ll keep investigating.

Robin | 29 Mar 2005
Gravatar

Should have waited a moment before commenting. I found a fix adding a single line in weighted_categories():
$cats = explode(“n”, $cats);
array_pop($cats); // this line !
foreach ($cats as $cat)

Robin | 29 Mar 2005
Gravatar

I don’t have an archives.php file…

can you be more specific about how to use this script?

Thanks

Heatsink | 4 Apr 2005
Gravatar

Any intention to convert the weighted-categories plugin into a weighted-keyword one?¿ Maybe to use along with the Wp-tags one?¿

elaine | 5 Apr 2005
Gravatar

Great little plugin! Bravo!

richard | 9 May 2005
Gravatar

hi, grate plugin!

i don’t know why is not working here: http://wearemad.weblogs.us/livearchives/

in the category space, it wrongs add the link… eny idea!? :(

task | 14 May 2005
Gravatar

Is there any code I can add so that only categories with a certain number of posts will show up with the plugin?

Charles | 23 Jun 2005
Gravatar

Hi

I’ve been using this little plugin for quite a while and have never had a problem with it under 1.5. However, I’ve just added a new category and it won’t display. Instead of using the standard http://www.ektopia.co.uk/ektopia/archives/category/reviews URL it shows http://www.ektopia.co.uk/ektopia/weighted-categories/Reviews but ONLY for that particular category and therefore points to a 404.

Any idea what might be going on?? Cheers

Reevo | 11 Jul 2005
Gravatar

eAk,

I have corrected the code. Now it works fine for me:

The only thing I had to do actually was to adjust tag font size to make the whole thing fit my sidebar. Check it out http://rodolphoarruda.pro.br/blog/

Rodolpho Arruda | 12 Jul 2005
Gravatar

I’ve been trying to get this to work, but I’ve hit a slight snag. The links generated point to /archives/categoryname instead of /category/categoryname.

My test WP install is at http://tin-men.net/wordpress

Cheers!

Graham | 21 Aug 2005
Gravatar

How do you do this without a list style? I just want a square block of words like in Flickr…I’m sure this is ridiculously easy…Thanks for the great plugin…

ryan | 25 Aug 2005
Gravatar

Thanks for the plug-in: I’m hoping for the same tweak as Ryan i.e. a flickry block of links. Best /FK

fkeegan | 26 Aug 2005
Gravatar

Nice plugin, I use it in conjunction with the cat2tag plugin which works very well. However it also generates hugh amounts of catagories. So I’ve changed your code slightly to show only the top x ($number) of catagories. As so:
/* Sort on counts */
arsort( $counts );

/* Only keep top $number */
$i=count( $counts )-$number;

for($j=1;$j

Robert de Bruin | 23 Sep 2005
Gravatar

Second half …
for($j=1;$j

Robert de Bruin | 23 Sep 2005
Gravatar

for($j=1;$j

Robert de Bruin | 23 Sep 2005
Gravatar

Sorry ’bout that … seems the & wasn’t converted correctly …
/* Sort on counts */
arsort( $counts );

/* Only keep top $number */
$i=count( $counts )-$number;

for($j=1;$j&=$i;$j++)
{
array_pop( $counts );
}

/* Sort alphabeticly again (case insensitive) */
uksort($counts, “strnatcasecmp”);

Robert de Bruin | 23 Sep 2005
Gravatar

Got mine working in the end, just upgraded from 1.5 to 1.5.2 and it started working again! Great plugin by the way :-)

Reevo | 23 Sep 2005
Gravatar

Howdy,

I’ve played around with your plug-in and added some features :
– Display the categories with different opacities (more posts = less transparents) and you can define a minimum opacity
– Display the categories with different colors

For the latter, you specify 2 colors (in #xxxxxx or #xxx format), the first will determine the color of your category that has less posts will have and the second will determine the color of your category that has more posts. You also specify the number of color your palette will have.

Okay, so I know my english sucks (especially at 4 a.m.) but go check my website @ http://www.autre-chose.org

I’ll clean the code in a few days and will make the plugin available for download.

Maxime Dupuis | 14 Oct 2005
Gravatar

Hey fella,
Nice plugin which finished off my website nicely http://www.diretribe.com however, there are a couple of bugs in the maths that thought I should let you know about.

When calculating the spread/step values the variables are the wrong way around, the correct ways should be as follows:

$mincount=min($counts);
$spread = max($counts) - min($counts);
if ($spread
Note, you need the $mincount for later when outputting as followed:

($smallest + ($fontstep * ($count-$mincount)))

By doing it this way the values of the font-sizes will always fall withn your range set in min/max (before that it didn't seem to).

An example:

font_spread: 10-20
count_spread: 1-3
count_min: 1

font_step = font_spread/count_spread = 10/2 = 5

e.g. where count = 2
5*(count-count_min)+minimum_font = 5*(2-1)+10 = 15

Compared to:

font_step=count_spread/font_spread = 2/10 = 0.2

e.g. where count=2
minimum_font+(count/font_step)=10+(2/0.2)=10=20

As you can see in the second example (using the current maths in the plugin) a half-way value is hitting the maximum. Hope that helps - if you need any help let me know.

Folowing Maxime Dupuis example above I've also knocked it into providing transparency & so on. Lovely. Thanks again for a great plugin.

Martin Fitzpatrick | 24 Oct 2005
Gravatar

I love what you did… I mean, the random CSS positionning with the transparency is so cool… except that some categories are sometimes inaccessible because under another. You should keep positions into an bi-dimensionnal array so categories must be X pixels from another (for both top and left positions). But other than that, great job ! I love your wordpress template too!

Maxime Dupuis | 24 Oct 2005
Gravatar

Can someone PLEASE!! post a 100% “WORKING!” WORDPRESS Version on this Tag script and also EXACT instructions such as which php file to include function call and step-for-step directions.
I have been many days going on weeks scouring this internet thru Google and all i ever find are people posting comments on nothing but one error after another, and i have personally downloaded many of these only to have them show nothing but errors myself and also never do any of you coders ever draw up step for step instructions.

Joseph | 27 Nov 2005
Gravatar

I’d agree. Pretty poor in the clarity department for those who might now speak 100% nerd code. I mean, people who develop these plugins do it so that others use them, right? Even if they might be someone who isn’t a certified programmer (ie. a designer). At any rate; I found success with Heat Map in case anyone notices. They have slightly better documentation … at least it’s apparent, instead of reading through all these comments.

Anonymous | 14 Dec 2005
Gravatar

You should add rel=”tag” in the print for the links.

Daniel | 15 Dec 2005
Gravatar

How do I make only top 10 or 15 categories to display?

Ramin | 14 Jan 2006
Gravatar

ramin i noticed on ur website u display only the top 15 or so categories could you tell me how u did that

steve | 25 Jan 2006
Gravatar

This is an excellent plugin. I have it working with WP2.0.1 and K2 beta2r163. Thank you so much. Maybe it’s me, but I think this on the homepage is a great ‘flavour’ for the content the site holds, so I’m delighted to now have yummy K2 and your weighted comments. Brilliant!

Alan | 1 Feb 2006
Gravatar

Has anyone noticed that the last link on the list is always incorrect? Even on the hitormiss.org site. Any fix?

Harshblogger | 8 Feb 2006
Gravatar

Look at the output of list_cats() – it include a newline at the end of the output, which then messes up the first foreach loop.

Change:
$cats = list_cats(1, ‘all’, ‘name’, ‘asc’, ”, 0, 0, 1, 1, 0, 1, 1, 0, 1, ”, ”, $exclude, 0);
to
$cats = trim(list_cats(1, ‘all’, ‘name’, ‘asc’, ”, 0, 0, 1, 1, 0, 1, 1, 0, 1, ”, ”, $exclude, 0));

… and all will be well.

Zak Grenat | 22 Feb 2006
Gravatar

Here is a cleaned up version of the code – Matt, feel free to use as you see fit. Cheers! –zak

function weighted_categories($smallest=10, $largest=48, $unit=”pt”, $exclude=”) {
$all_cats = trim(list_cats(1, ‘all’, ‘name’, ‘asc’, ”, 0, 0, 1, 1, 0, 1, 1, 0, 1, ”, ”, $exclude, 0));

foreach (explode(“n”, $all_cats) as $cat) {
preg_match(‘{a href=”([^"]+)”.+?>(.*) ?((d+))}’, $cat, $matches);
list(,$url, $name, $count) = $matches;
printf(‘%s‘.”n”, $url, $count, abs($count) != 1 ? ‘entries’ : ‘entry’, $count, $name);
$counts[$count] = 1;
}

$counts = array_keys($counts);
$step = (max($counts) – min($counts)) / ($largest – $smallest);

echo ”;
foreach($counts as $count){
echo “.tcloud-$count {font-size: ” . ($smallest + ($step * $count)) . $unit . ” }n”;
}
echo ”;
}

Zak Greant | 22 Feb 2006
Gravatar

I have further hacked up the plugin – vist http://zak.greant.com/2006/02/23/category-cloud-plugin/ to grab a copy.

Zak Greant | 23 Feb 2006
Gravatar

Hey, just wanted to say that I’m using tihs plug on my site! Thanks a lot!

http://www.the-tempest.net

Riley Dutton | 25 Feb 2006
Gravatar

Thank you Zak. Your fix did the trick. It’s funny… looks like Matt hasn’t even noticed it it.

Harshblogger | 2 Mar 2006
Gravatar

Great plugin! Would it be possible to give it the ability to only display a certain number of top catagories? I’d really like to go crazy with “tagging” my posts, but I don’t want a lot of catagories with only one or two entries taking up space on my sidebar. It would be nice to tell the plugin to only show the top 25 catagories, etc.

Thanks nonetheless!

Jason | 13 Apr 2006
Gravatar

I had the same problem with the last category not working, and Zak’s fix worked like a charm. Thanks!

Beyond that, this is a really great plug-in. Simple, fast, and effective – exactly what I was looking for.

Samuel | 15 Apr 2006
Gravatar

I had some troubles with this plugin, but now it works fine!
You wrote:

“In the file where you want to display the weighted list of categories (ex. archives.php), insert the following code:

But it that doesn’t works in my blog.

I’ve changed
to –>

So I add the letters “php” between

Mark Jongkind | 16 Apr 2006
Gravatar

I love this plugin! I just put it on my blog!

I added it to the sidebar.php file for the fasttrack theme because that theme doesn’t use archive.php

Garrett Wilkin | 17 Apr 2006
Gravatar

Great plugin, got it in my footer. Works like a charm. One request, could this be implemented for the monthly archives, and if so how would I go about doing that?

Lawsy | 13 May 2006
Gravatar

Beau boulot, ce plugin fonctionne très bien chez moi :-)
Good job.

ResuX | 22 Jun 2006
Gravatar

this is an amazing plugin, but, have you noticed the performance hits for blogs with large amounts of categories? it’s almost to bring down apache.

nerds | 19 Aug 2006
Gravatar

hello… i got this plugin not working on my blog…. it keep print this code, not execute it…

someone pls help me!!11`~~!!!

paskal | 20 Aug 2006
Gravatar

hi: does not work after installed:

Warning: Cannot modify header information – headers already sent by

pls help me

cyberico | 5 Sep 2006
Gravatar

I’ve just installed it – great work, excellent little plugin, thanks! :) now if only I can get my stylesheets working properly ;)

Joe | 16 Oct 2006
Gravatar

PS – for people who can’t get it to work, change

Joe | 16 Oct 2006
Gravatar

oops – change the top line from
-less than- -question mark-
to
-less than- -question mark- -php-
with no spaces!

Joe | 16 Oct 2006
Gravatar

Hey guys, thanks for all the dialogue and fixes. But with everything said, for a newbie like me, I am completely lost. Could anyone just write a quick rundown of the steps I have to follow to install the plugin the proper way in my wordpress blog? I would love to use this. Thanks

Sam | 5 Nov 2006
Gravatar

it would be great to have this functionality packaged as a widget.

craig buchanan | 29 Nov 2006
Gravatar

i packaged your code as a widget. email me at cra ig. buch anan at cogniza dot com for the code.

btw, where which CSS did you alter? the style.css?

craig buchanan | 29 Nov 2006
Gravatar

Great plugin, I really love it.

Any way you can port this to WordPress 2.1 that was released a few hours ago?

Jonathan | 22 Jan 2007
Gravatar

here’s a link to the widget: http://www.cogniza.com/blog/?p=93

craig buchanan | 23 Jan 2007
Gravatar

Agee with Jonathan, will be cool to port this plugin to WordPress 2.1
I’m looking forward for this…

curdaneta | 30 Jan 2007
Gravatar

Here’s another vote for an update. Since they broke list_cats() it’s not working, and I have no clue how to modify the plugin to get it working with the new wp_list_categories().

Jerry | 1 Feb 2007
Gravatar

hi!
I wrote an updated version that works for wordpress 2.1.
It hase some new features and better wordpress integration (i.e. option page), it’s called Category Cloud :)

francesco mapelli | 4 Feb 2007
Gravatar

it stripped the address… here it is: http://www.mapelli.info/wordpress/category-cloud-10

francesco mapelli | 4 Feb 2007
Gravatar

I’m using this in WP 2.0.x and it works great except that parent categories with sub-cats in them, aren’t linking properly. All the sub-cats link, but the parent category does this: mywebsite.com/the-category-name instead of mywebsite.com/categories/the-category-name. What do I need to do to fix?

auschick | 6 Feb 2007
Gravatar

I’m on WP 2.1.2 using ‘whiteasmilk’ and I just can’t get it to work. :-( Any chance for an update?

In any case – thanks for sharing!

SF

Scudfish | 12 Apr 2007
Gravatar

[...] a sidebar widget to display the categories as a tag cloud. Based on Matt Kingston’s Weighted Categories WP [...]

Category Cloud Widget » My Web Stuff | 21 Nov 2008
Gravatar

Hello.
I don't want to use the categories-list, I want to show the list by keywords like you have on your site. Is it something that can be a simply changed in the code I insert in the page.
Or maybe is going to be a another plugin? Let me know! I must apologize for my bad english, have a nice day.

//Örjan

(thinking of this code)

Oerjan | 4 Nov 2009
Gravatar

[...] Weighted Categories: Muestra una lista de las categorías del sitio web con un tamaño proporcional al número de entradas en esa categoría. [...]

Plugins para Wordpress. | Puydi Publicidad 100% efectiva | 5 Jan 2010
Gravatar

[...] Description: Adds a sidebar widget to display the categories as a tag cloud. Based on Matt Kingston’s Weighted Categories WP plugin. Author: Lee Kelleher Version: 1.0 Author URI: http://vertino.wordpress.com/ [...]

nubesss « No te alienes | 8 Jan 2010
Gravatar

[...] Weighted Categories: Muestra una lista de las categorías del sitio web con un tamaño proporcional al número de entradas en esa categoría. [...]

Plugins para Wordpress de la A a la Z. | La Fanega | 13 Jan 2010
Gravatar

[...] be indicated by varying font sizes, color intensities or both. This plugin was largely inspired by Matt Kingston’s Weighted Categories [...]

Heat Map plugin – KeganBall.net | 8 Apr 2010
Gravatar

[...] Weighted Categories [...]

Chris Miller – A new kinna archive | 24 May 2013

Respond