Programmish
Posts Tagged php
Dropdown tag menu in WordPress

The only times I end up working with PHP are when modifying a WordPress template to add new functionality or somehow tweak existing functionality. Most of the time this is a straightforward process; the WordPress functions and code layout are surprisingly clear, and tend to take care of obvious tasks for you, without having to munge around too much data by hand.

Alas, in this case I wanted to deviate from the standard output of a function, notably the wp_tag_cloudhttp://codex.wordpress.org/Template_Tags/wp_tag_cloud method. Rather than an ordered or unordered list, I wanted to display all of the tags used in the blog in a single drop down list. I didn’t care how many posts were tagged with each, I just wanted a drop down list that would navigate to a tag archive. A little bit of digging, and some experimentation lead to the following code snippit:

  	<select name="tag-dropdown" onChange="document.location.href='<?php bloginfo('url'); ?>/?tag=' + this.options[this.selectedIndex].value;">
  		<option value=""><?php echo attribute_escape(__('Browser Archive by Tag')); ?></option>
  		<?php
  			$args = array(
	                        'number' => 0,
	                        'format' => 'array',
	                        'taxonomy' => 'post_tag'
	                );
	                $tags = get_terms( $args['taxonomy'], args); 

  			foreach ($tags as $key => $tag ) {
  				$term = &get_term($tag->term_id, 'post_tag');
  				echo "<option value='" . $tag->slug . "'>" . $term->slug . "</option>";
  			}
  		?>
  	</select>

The results are a simple alphabetically ordered drop down of Tags used in the blog.