Create a Dropdown List of Tags

One of the options on the default category widget is to display it as a dropdown. When a category is selected, user is immediately redirected to the category archive page.

This functionality is provided by the wp_dropdown_categories function. Surprisingly, this same function can be used to display a list of tags as well (which, design-wise, seems to be a better alternative than a tag cloud).

Here’s the code, modified from the example available on the Codex page:

<li id="categories">
	<h2><?php _e('Posts by Tags'); ?></h2>
	<form action="<?php bloginfo('url'); ?>/" method="get">
	<div>
<?php
$select = wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
	<noscript><div><input type="submit" value="View" /></div></noscript>
	</div></form>
</li>

Above, note the taxonomy=post_tag parameter that tells the function to generate a list of tags. If you have your own taxonomy, you can use that too. Pretty nifty!

You may also like...

css.php