Display custom taxonomy terms in a dropdown menu

June 3, 2010 | Wordpress tutorials | 3 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.


3 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/

Leave a Reply