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.

Leave a Reply

Privacy & more..

Cookie Consent Policy