Designed for Wordpress 2.1 - Display a weighted list of category links. Mods for coloured/weighted list by Dean Robinson. Based on Weighted Categories Plugin by Matt Kingston Version: 2.0 Author: Dean Robinson Author URI: http://www.deanjrobinson.com/ */ function weighted_categories($weighted, $colored, $smallest=70, $largest=250, $unit="%", $lightest="#FFFFFF", $darkest="#666666", $exclude='') { $cats = get_all_cats(1, 'all', 'name', 'asc', '', false, 0, 1, 1, 1, true, 0, 0, 1, '', '', $exclude, false); // list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) $cats = explode("\n", $cats); foreach ($cats as $cat) { eregi("a href=\"(.+)\" ", $cat, $regs); $catlink = $regs[1]; $cat = trim(strip_tags($cat)); eregi("(.*) \(([0-9]+)\)$", $cat, $regs); $title = preg_replace('(\s\(([0-9]+)\))','', $cat); $catname = $title; $count = $regs[2]; $counts{$catname} = $count; $catlinks{$catname} = $catlink; } $spread = max($counts) - min($counts); if ($spread <= 0) { $spread = 1; }; $fontspread = $largest - $smallest; $fontstep = $spread / $fontspread; if ($fontspread <= 0) { $fontspread = 1; } $lightred = hexdec(substr($lightest,1,2)); $darkred = hexdec(substr($darkest,1,2)); $redspread = $lightred - $darkred; if($redspread <=0) { $redspread = $darkred - $lightred; } $redstep = $redspread / $spread; $lightgreen = hexdec(substr($lightest,3,2)); $darkgreen = hexdec(substr($darkest,3,2)); $greenspread = $lightgreen - $darkgreen; if($greenspread <=0) { $greenspread = $darkgreen - $lightgreen; } $greenstep = $greenspread / $spread; $lightblue = hexdec(substr($lightest,5,2)); $darkblue = hexdec(substr($darkest,5,2)); $bluespread = $lightblue - $darkblue; if($bluespread <=0) { $bluespread = $darkblue - $lightblue; } $bluestep = $bluespread / $spread; foreach ($counts as $catname => $count) { $scale = $count;// / ( $spread + 1); // * ($colorstep); $catlink = $catlinks{$catname}; print "$catname\n"; } } /* adapted from Ultimate Tag Warrior Plugin for tag coloring */ function GetColorForWeight($scale,$darkred,$darkgreen,$darkblue,$redstep,$greenstep,$bluestep,$redspread,$greenspread,$bluespread) { if ($scale) { $redscale = $scale*$redstep; $greenscale = $scale*$greenstep; $bluescale = $scale*$bluestep; $r = dechex(intval( $darkred + $redscale )); $g = dechex(intval( $darkgreen + $greenscale )); $b = dechex(intval( $darkblue + $bluescale )); if (hexdec($r) >= 255) $r = "FF"; if (hexdec($g) >= 255) $g = "FF"; if (hexdec($b) >= 255) $b = "FF"; if (strlen($r) == 1) $r = "0" . $r; if (strlen($g) == 1) $g = "0" . $g; if (strlen($b) == 1) $b = "0" . $b; return "#$r$g$b"; } } /* COPY OF list_cats() FROM wp-includes/template-functions-category.php ...I don't know why, but it wouldn't work without it ?? */ function get_all_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) { global $wpdb, $wp_query; // Optiondates now works if ( '' == $file ) $file = get_settings('home') . '/'; $exclusions = ''; if ( !empty($exclude) ) { $excats = preg_split('/[\s,]+/',$exclude); if ( count($excats) ) { foreach ( $excats as $excat ) { $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; } } } $exclusions = apply_filters('list_cats_exclusions', $exclusions ); if ( intval($categories) == 0 ) { $sort_column = 'cat_'.$sort_column; $query = " SELECT cat_ID, cat_name, category_nicename, category_description, category_parent, category_count FROM $wpdb->categories WHERE cat_ID > 0 $exclusions ORDER BY $sort_column $sort_order"; $categories = $wpdb->get_results($query); } if ( $optiondates ) { $cat_dates = $wpdb->get_results(" SELECT category_id, UNIX_TIMESTAMP( MAX(post_date) ) AS ts FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories WHERE post_status = 'publish' AND post_id = ID $exclusions GROUP BY category_id"); foreach ( $cat_dates as $cat_date ) { $category_timestamp["$cat_date->category_id"] = $cat_date->ts; } } $num_found=0; $thelist = ""; foreach ( (array) $categories as $category ) { if ( ( intval($hide_empty) == 0 || $category->category_count) && (!$hierarchical || $category->category_parent == $child_of) ) { $num_found++; $link = 'category_description) ) $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"'; else $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"'; $link .= '>'; $link .= apply_filters('list_cats', $category->cat_name, $category).''; if ( (! empty($feed_image)) || (! empty($feed)) ) { $link .= ' '; if ( empty($feed_image) ) $link .= '('; $link .= ''; else $link .= $name; $link .= ''; if (empty($feed_image)) $link .= ')'; } if ( intval($optioncount) == 1 ) $link .= ' ('.intval($category->category_count).')'; if ( $optiondates ) { if ( $optiondates == 1 ) $optiondates = 'Y-m-d'; $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]); } if ( $list ) { $thelist .= "\tcat_ID == $wp_query->get_queried_object_id()) && is_category()) { $thelist .= ' class="current-cat"'; } $thelist .= ">$link\n"; } else { $thelist .= "\t$link
\n"; } if ($hierarchical && $children) $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical); if ($list) $thelist .= "\n"; } } if ( !$num_found && !$child_of ) { if ( $list ) { $before = '
  • '; $after = '
  • '; } echo $before . __("No categories") . $after . "\n"; return; } if ( $list && $child_of && $num_found && $recurse ) { $pre = "\t\t\n"; } else { $pre = $post = ''; } $thelist = $pre . $thelist . $post; if ( $recurse ) return $thelist; echo apply_filters('list_cats', $thelist); } ?>