Tag Cloud - code Rewrite
My Tag cloud implementation started from the Matt’s weighted-categories implementation on hitormiss. It was working fine but I needed two things (yeah, just 2)
- the hover text to be more informative
- WordPress 2.1 support
I only needed to change one line to make the hover text include the category name (for cases where my font size was tiny). However, all went out the window when I upgraded to WP 2.1. Most of the weighted-categories code had been deprecated over the 2 years since it was first authored. So, I did some digging and figured out the new implementation.
Here is Matt’s code ported to WP 2.1. I still want to replace the regular expression calls with Perl compatible regular expressions for performance reasons.
1: <?php
2:
3: function weighted_categories($smallest=10, $largest=48, $unit="pt", $exclude='')
4: {
5: // the new WP function 'wp_list_categories' outputs the cats for you
6: // so it needs to be changed to support 'echo=0'
7: $cats = wp_list_categories ('orderby=name&show_count=1&hierarchical=0&echo=0&title_li=0&exclude='
. $exclude);
8:
9: $cats = explode("n", $cats);
10: array_pop($cats);
11: foreach ($cats as $cat) {
12: if (eregi("a href=\"(.+)\" ", $cat, $regs)) {
13: $catlink = $regs[1];
14: $cat = trim(strip_tags($cat));
15: eregi("(.*) (([0-9]+))$", $cat, $regs);
16: $catname = $regs[1]; $count = $regs[2];
17: $counts{$catname} = $count;
18: $catlinks{$catname} = $catlink;
19: }
20: }
21:
22: $spread = max($counts) - min($counts);
23: if ($spread <= 0) { $spread = 1; };P
24: $fontspread = $largest - $smallest;
25: $fontstep = $spread / $fontspread;
26: if ($fontspread <= 0) {
27: $fontspread = 1;
28: }
29:
30: foreach ($counts as $catname => $count) {
31: $catlink = $catlinks{$catname};
32: print "<a href=\"$catlink\" title=\"$catname has $count entries\" style=\"font-size: "
. ($smallest + ($count/$fontstep)) . "$unit;\">$catname</a> n";
33: }
34: }
35:
36: ?>




March 7th, 2007 at 04:30
Hmm, there seems to be something wrong in line 7 (13 on my installation):
Parse error: syntax error, unexpected ‘=’ in /(myhomedir)/blog/wp-content/plugins/weighted_categories.php on line 13
I don’t “speak” PHP, however… any ideas?
March 7th, 2007 at 10:59
Looks like “sortby” has been removed but was not documented as part of the deprecated code. It should be “orderby”
March 9th, 2007 at 11:28
No, this doesn’t seem to be the reason, I keep getting the error…
March 9th, 2007 at 12:25
have you made the change to the WordPress core code for wp_list_categories(0 to support the “echo” option ? (the function does not have this option but it is needed to use the function as a replacement for the old / deprecated code. - See comments on line 5&6)
March 9th, 2007 at 12:52
Oh, I understood that comment differently (I thought adding echo=0 to line 7 *was* the patch). What changes to the core code are needed?
March 9th, 2007 at 12:54
[...] tut seit dem Update auf Wordpress 2.0 nicht mehr. Der von einem anonymen Blogger vorgeschlagene Patch für das Plugin funktionierte bei mir leider nicht, aber der Autor bringt mir gerade geduldig die [...]
March 9th, 2007 at 15:20
The function wp_list_categories (./wp-includes/category-template.php) does not have an option for ‘echo’. If you look at wp_dropdown_categories() (in the same module), you see it has an option for ‘echo’.
The change I needed in wp_list_categories() was to add the echo option to the $defaults array - add: ‘echo’ => true
Then, at the end of the function, make the echo optional as follows:
// allow echo to be turned off
$output = apply_filters(’wp_list_categories’, $output);
if ($echo)
echo $output;
return $output;
March 13th, 2007 at 06:20
Thanks for this information; I tried it out and it worked fine!
March 13th, 2007 at 09:29
glad to hear it is working for you. I should make a request to WordPress.org to make a provision for wp_list_categories() so it has compatible function to the deprecated code.