• Does anyone knows how to display terms of a custom taxonomy in a dropdown menu, complete with the post count?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi bedex78,

    You can use this:
    http://codex.wordpress.org/Template_Tags/wp_dropdown_categories

    example:
    <?php wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=1&taxonomy=custom_taxonomy_name');

    I don’t really like that one, though, because you are forced to use the taxonomy ID as the value, which, if you are using it for a jumpmenu, can make it difficult to work with custom taxonomy templates

    I just wrote the function below which uses get_terms:
    http://codex.wordpress.org/Function_Reference/get_terms

    It uses the get_terms arguments (see codex link above) by passing them through the $args array.

    Advantages:
    – creates easy to use links to taxonomy templates
    – you can add multiple taxonomies to the dropdown list

    Disadvantages: I haven’t figgred out how to add ‘count’ yet, along with other parameters that wp_dropdown_categories offers.

    <?php
    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$output ="<select>";
    	foreach($myterms as $term){
    		$root_url = get_bloginfo('url');
    		$term_taxonomy=$term->taxonomy;
    		$term_slug=$term->slug;
    		$term_name =$term->name;
    		$link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
    		$output .="<option value='".$link."'>".$term_name."</option>";
    	}
    	$output .="</select>";
    return $output;
    }
    
    $taxonomies = array('custom_taxonomy_name');
    $args = array('orderby'=>'count','hide_empty'=>true);
    echo get_terms_dropdown($taxonomies, $args);
    
    ?>

    Hope this helps!

    Okay, I tried the default code using the wp_dropdown_categories function. Problem is, though, that the links for my the custom taxonomies don’t work. The link structure is completly wrong.

    The wp_dropdown_categories function returns links that look like:
    bloginfo(url)/?cat=ID, but the links to the custom taxonomies look actually like bloginfo(url)/?custom_taxonomy_name=slug (I set the rewrite part in functions.php to false).

    When I set ‘name’ => ‘custom_taxonomy_name’ for wp_dropdown_categories, at least it replaces the ‘?cat’ part with the correct ‘?custom_taxonomy_name’ part. But instead of the slug, it still uses the ID and that results in a link error then.

    I don’t know if this some kind of permalink, rewrite rules or error of the drop-down code for the wp_dropdown_categories function.

    I tried your code, earthmanweb, but while it returns the drop-down, nothing happens upon selecting a value.

    Frustrating so far. So any hints are very welcome.

    You can get term link with get_term_link() – first arg is term id, second taxonomy. So you can replace this:

    $root_url = get_bloginfo('url');
    $term_taxonomy=$term->taxonomy;
    $term_slug=$term->slug;
    $term_name =$term->name;
    $link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;

    With

    $link = get_term_link($term->term_id, $term->taxonomy);

    But here’s how I did this:

    <form action="<?php bloginfo('url'); ?>" method="get">
    	<div>
    <?php
    $taxonomies = array('TAXONOMY NAME');
    $args = array('orderby'=>'name','hide_empty'=>true);
    $select = get_terms_dropdown($taxonomies, $args);
    
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>
    	<noscript><div><input type="submit" value="Näytä" /></div></noscript>
    	</div></form>

    Combine this with the get_terms_dropdown function described above, with couple of changes:

    function get_terms_dropdown($taxonomies, $args){
    	$myterms = get_terms($taxonomies, $args);
    	$output ="<select name='TAXONOMY SLUG'>";
    	foreach($myterms as $term){
    		$root_url = get_bloginfo('url');
    		$term_taxonomy=$term->taxonomy;
    		$term_slug=$term->slug;
    		$term_name =$term->name;
    		$link = $term_slug;
    		$output .="<option value='".$link."'>".$term_name."</option>";
    	}
    	$output .="</select>";
    return $output;
    }

    This isn’t the cleanest solution (and only works with one taxonomy at the time), but it does the trick. It uses a wrong url, but the rewrite should pass the user to the correct page.

    Adding the term count is easy enough – it’s in $term->count, so just add that to the $output like this

    $output .="<option value='".$link."'>".$term_name." (".$term->count.") </option>";

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Terms of Custom Taxonomy in a Dropdown Menu’ is closed to new replies.