Display custom taxonomy terms in a dropdown menu

June 3, 2010 | Wordpress tutorials | 8 Comments »


Sometimes I want to dislay my taxonomy terms in a different way, so today you will learn how to make a dropdown menu from custom taxonomy terms.

Firstly you should go to your theme directory and create new folder, for example lib. In lib folder create a file jumpto.php and paste code below:

<?php
// Solution for displaying custom taxonomy terms in a dropdown menu.
function dropdown_tag($taxonomy, $args = '' ) {
	$defaults = array('taxonomy' => 'my_taxonomy', // <- write taxonomy name
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
		'exclude' => '', 'include' => ''
	);
	$args = wp_parse_args( $args, $defaults );

	$terms = get_terms($taxonomy, array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) );

	if ( empty($terms) )
		return;

	$return = generate_dropdown_tag( $terms, $taxonomy, $args );
	if ( is_wp_error( $return ) )
		return false;
	else
		echo apply_filters( 'dropdown_tag', $return, $args );
}

function generate_dropdown_tag( $terms, $taxonomy, $args = '' ) {
	global $wp_rewrite;
	$defaults = array(
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
	);
	$args = wp_parse_args( $args, $defaults );
	extract($args);

	if ( !$terms )
		return;
	$counts = $term_links = array();
	foreach ( (array) $terms as $term ) {
		$counts[$term->name] = $term->count;
		$term_links[$term->name] = get_term_link( $term->name, $taxonomy );
		if ( is_wp_error( $term_links[$term->name] ) )
			return $term_links[$term->name];
		$term_ids[$term->name] = $term->term_id;
	}

	$min_count = min($counts);
	$spread = max($counts) - $min_count;
	if ( $spread <= 0 )
		$spread = 1;
	$font_spread = $largest - $smallest;
	if ( $font_spread <= 0 )
		$font_spread = 1;
	$font_step = $font_spread / $spread;

	// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
	if ( 'name' == $orderby )
		uksort($counts, 'strnatcasecmp');
	else
		asort($counts);

	if ( 'DESC' == $order )
		$counts = array_reverse( $counts, true );

	$a = array();

	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="term"' : '';

	foreach ( $counts as $term => $count ) {
		$term_id = $term_ids[$term];
		$term_link = clean_url($term_links[$term]);
		$term = str_replace(' ', '&nbsp;', wp_specialchars( $term ));
		$a[] = "\t<option value='$term_link'>$term ($count)</option>";
	}

	switch ( $format ) :
	case 'array' :
		$return =& $a;
		break;
	case 'list' :
		$return = "<ul class='wp-tag-cloud'>\n\t<li>";
		$return .= join("</li>\n\t<li>", $a);
		$return .= "</li>\n</ul>\n";
		break;
	default :
		$return = join("\n", $a);
		break;
	endswitch;

	return apply_filters( 'generate_dropdown_tag', $return, $term, $args );
}
?>

Edit line 4th and save.


Now open functions.php file and write after opening php tag:

define('THEMELIB', TEMPLATEPATH . '/lib');

require_once(THEMELIB . '/jumpto.php');

All set :)
Go to your themes file for example sidebar.php and paste this code:

<select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="#">Select...</option>
<?php dropdown_tag('my_taxonomy', 'number=0&order=asc'); ?>
</select>

Edit 3rd line and save.


Thats all, now you have taxonomy terms in a drop down menu.


8 Responses

  1. Hugo says:

    First congratulations for your website, its just amazing, you have all the best tutorials about wordpress 3.0.

    Your code works like a charm, but what about multiple taxonomies dropdown? Like if you have one taxonomy named cinema and from there you shown the hierarchical taxonomies, like citys. Ex:

    Cinema:
    -New York
    - Chicago

    Theater
    - New York
    - Chicago

    And then have: in one page and have:
    in another page.

    I tried to duplicating the jumpto.php file, changing the name and adding another line in the functions, just to see the error eh, and it gave me: Cannot redeclare dropdown_tag(), of course. So maybe there is a way to acomplish this, now iam exploring the source code of this plugin: http://wordpress.org/extend/plugins/nktagcloud/.

    Is it possible to change your code to have multiple taxonomies dropdowns?

  2. Hugo says:

    Sorry i added some come that didnt show in the comment. Like i was saying…

    In one page have: dropdown_tag cinema order=asc
    and in other page: dropdown_tag theater order=asc

  3. slashas says:

    Hi Hugo,
    I wrote mini tutorial about multiple taxonomies dropdown:
    http://www.aroundwp.com/multiple-taxonomies-in-a-dropdown-menus/

  4. Gian says:

    Hi, I am using your code and find it very useful for what I needed.

    I just got a little problem with it. I am using the dropdown for some “events” custom types, and i set a query to display only the event that are to come, with a custom type, like so:

    &meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC');
    ?>

    Now, is there a way to adapt your code so that it pulls "terms" only from those "events" with the custom field not older that the today's date?

    Thanks in advance.

  5. lucky_girl says:

    I think you are good at creating a drop down menu, . so i really need your help, i want to create a dropdown menu for my website, but i have no experience, can you recommand some tools or books about it? Thanks to you!

  6. Wendy says:

    Hi — thanks for a wonderful tutorial that is a solution I really need. Everything is working perfectly, except that the results page shows all of the taxonomy, not just the selected one. Oddly, the count for the taxonomy terms is correct.

    I am using custom post type, but I was able to get the dropdown drawing from that type. It’s picking up the custom taxonomy that only appears in that custom post type, but displaying all results for the entire custom taxonomy.

    Any ideas? I am so close.

  7. Mark says:

    I’m with Gian and Wendy….can it be limited to one category or custom post type?

  8. Musti says:

    Is there any way to filter retrieved terms by a custom category?
    Thanks in advance

Leave a Reply