Return to Snippet

Revision: 32478
at September 26, 2010 03:57 by fjarrett


Initial Code
function dropdown_taxonomy_term( $taxonomy, $args = '' ) {
	$defaults = array(
		'orderby' => 'name',
		'order' => 'ASC',
		'hide_empty' => 1,
		'format' => 'flat',
		'number' => 45,
		'hide_empty' => 1,
		'fields' => 'all',
		'slug' => '',
		'hierarchical' => 1,
		'name__like' => '',
		'pad_counts' => 0,
		'get' => '',
		'child_of' => 0,
		'parent' => ''
	);
	
	$args = wp_parse_args( $args, $defaults );

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

	if ( is_wp_error( $return ) )
		return false;
	else
		echo apply_filters( 'dropdown_taxonomy_term', $return, $args );
}


function dropdown_generate_taxonomy_term( $terms, $taxonomy, $args = '' ) {
	global $wp_rewrite;

	$defaults = array(
		'orderby' => 'name',
		'order' => 'ASC',
		'hide_empty' => 1,
		'format' => 'flat',
		'number' => 45,
		'hide_empty' => 1,
		'fields' => 'all',
		'slug' => '',
		'hierarchical' => 1,
		'name__like' => '',
		'pad_counts' => 0,
		'get' => '',
		'child_of' => 0,
		'parent' => ''
	);

	$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;

	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-taxonomy-term'>\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( 'dropdown_generate_taxonomy_term', $return, $terms, $args );
}

Initial URL
http://snipplr.com/view/41042/custom-taxonomy-terms-dropdown-template-tag/

Initial Description
The dropdown taxonomy term function will enable you to show custom taxonomies in a dropdown menu. It supports both hierarchal (category-type) and non-hierarchal (tag-type) taxonomies.

Initial Title
Custom Taxonomy Terms Dropdown Function

Initial Tags
wordpress

Initial Language
PHP