Got bored and scratched another itch I’ve had in wordpress for a while. I always wanted a way to list my categories on the sidebar on my terms. The best I had found was the well written and executed Category Visibility-RH but I wanted to choose from all my categories and still limit the number listed.
I added a function to my wp-hacks.php and then dropped it into my sidebar. Does the job, and could easily be edited. I swear someday I’ll learn how to make plug-ins, really.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function jh_random_cats($howMany) { global $wpdb ; $query = " SELECT cat_ID, cat_name, category_count FROM $wpdb->categories WHERE category_count != '0' "; $categories = $wpdb->get_results($query); shuffle($categories); // Mix 'em up for($howMany; $howMany > 0; $howMany--) { $poppedCat = array_pop($categories); print '<li><a href="'.get_category_link($poppedCat->cat_ID).'">'.$poppedCat->cat_name.'</a> ('.$poppedCat->category_count.')</li>'; } } |
As a super-cool extra I got to use a PHP function I had never gotten to use before, let alone knew existed: bool shuffle ( array &array ). It, you guessed it, shuffles up an array.
Posted February 5th, 2007 - Permalink