Wordpress: Drop down categories

Note: If you’re looking for how to sort your category list I’ve posted that here.

Update: 10-23: Fixed another typo! Sometimes the way wordpress filters posts can be really annoying!
Update 10-22: I’ve fixed a few typos and things that got screwed up by Wordpress when I posted. :)

I just finished a quick hack for Wordpress (I don't use it on this site) and thought I'd post it here for everyone else.

There may be an easier/better way to do it, but this is how I did it. Let me know if there's a better way.

You'l need to edit two files. template-functions-category.php and index.php Be sure to make backups of these files before changing anything!!

template-functions-category.php

In this file there's a function:

function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',  
        $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,  
        $selected=0, $hide=0) {

Add another parameter (make sure it's the first one):

function dropdown_cats($indexpage = false, $optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',  
        $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,  
        $selected=0, $hide=0) {

This new parameter is needed so the index.php page can use it while leaving the functionality of the admin area to what it should be.

Then scroll down to the line:

echo "<select name='cat' class='postform'>\n";

And replace it with:

if ($indexpage == true) {  
   echo "<select name='cat' class='postform' onchange='document.location=options[selectedIndex].value;'>\n";  
} else {  
   echo "<select name='cat' class='postform'>\n";  
}

Then find this line:

echo "\t<option value=\"".$category->cat_ID."\"";

And replace it with:

if ($indexpage == true) {
   $link = get_category_link(0, $category->cat_ID, $category->category_nicename);  
   echo "\t<option value=\"".$link."\"";  
} else {  
   echo "\t<option value=\"".$category->cat_ID."\"";  
}

index.php

Change:

<ul>  
  <?php wp_list_cats(); ?>  
</ul>

To:

<form action="">  
   <?php dropdown_cats(true); ?>  
</form>

And that's it!

As a side note, I didn't do it this way, but I should have! So I haven't tested this to see if it works. If there's a code screw up or something I appologize. I've gotta run and don't have time to make sure it works. It's pretty straight forward though I think if you know some PHP. Let me know if there's a problem with it.

Update: it works.

← 
 →
Programming