Loading...

Ever needed to display your WordPress tags in a dropdown box?
Well here’s a handy function to do just that.

Let’s break it down, we use the get_tags() WordPress function to create an array containing all the tag data, then use a foreach loop to process each tag and return the necessary html.

Here’s the function code to paste into the functions.php file of your theme.

function dropdowntags(){
  if ($tags = get_tags( array('orderby' => 'name') )) {
   echo '<div id="tagform" align="right"><form action="'.get_bloginfo('url').'" method="get">';
   echo '<select name="tag" id="tag" class="postform">';
    foreach ($tags as $tag)
	{
	echo '<option value="'.$tag->slug.'">'.$tag->name.'</option>';
	}
  echo '</select> ';
  echo '<input type="submit" name="submit" value="view" id="tagbutton"/>';
  echo '</form></div>';
  }
}

You call the function from your page by inserting the following code in one of your templates, I placed it in the categories page (categories.php) next to the Page Title.

<?php dropdowntags(); ?>

and did a bit of styling on form and button (you will more than likely want to edit these)…

#tagform{
display: block;
float: right;
height: 50px;
margin-right: 20px;
}
#tagbutton {
color: red;
font-weight: bold;
border-bottom: 2px solid red;
width: 20px;
padding: 2px 5px 3px 5px;
}

And there you have it, a nice dropdown box you can place virtually anywhere in your wordpress site.

Latest Posts

Portfolio

Contact

Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.

 

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close