Categories Dropdown Menu
-
Here’s a sweet little dropdown menu that honors your subcategories and shows the reader what catergory they currently in. I’m sure there is a prettier way to do this but it works!
Paste this into index.php where you want the menu:
<form action= “<?php echo $PHP_SELF ?>” method=”get”>Current Category Selected:
<select name=’cat’ class=’postform’>
<option value=’0′><?php wp_title(); ?></option>
<?php wp_dropdown_cats(); ?>
</select>
<input type=”submit” name=”submit” value=”view” />
</form>
Then paste this near the top of index.php (above “<head profile=”https://gmpg.org/xfn/1″>”):
<?php function wp_dropdown_cats($currentcat, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
global $wpdb, $tablecategories, $tablepost2cat, $bgcolor;
if (!$categories) {
$categories = $wpdb->get_results(“SELECT * FROM $tablecategories ORDER BY cat_name”);
}
if ($categories) {
foreach ($categories as $category) { if ($currentcat != $category->cat_ID && $parent == $category->category_parent) {
$count = $wpdb->get_var(“SELECT COUNT(post_id) FROM $tablepost2cat WHERE category_id = $category->cat_ID”);
$pad = str_repeat(‘– ‘, $level);
echo “\n\t<option value=’$category->cat_ID'”;
if ($currentparent == $category->cat_ID)
echo ” selected=’selected'”;
echo “>$pad$category->cat_name</option>”;
wp_dropdown_cats($currentcat, $currentparent, $category->cat_ID, $level + 1, $categories);
} }
} else {
return false;
}
}
?>
Enjoy!
- The topic ‘Categories Dropdown Menu’ is closed to new replies.