Displaying taxonomy name in taxonomy term results

This custom code is to display the taxonomy name along with the term results.

What is this, and where do I put this custom code?

add_filter('asp_results', 'asp_display_tax_name_in_results');
function asp_display_tax_name_in_results($results) {
	// Change these variables according to the comments after
	$position = 'before'; 	// 'before' or 'after'
	$field = 'title';		// 'title' or 'content'
	$delimiter = ' - '; 	// Characters between the taxonomy name and the field

	// --- DO NOT CHANGER ANYTHING BELOW ---
	foreach($results as $k=>&$r){
		if ( $r->content_type == 'term' ) {
			$taxonomy = get_taxonomy( $r->taxonomy );
			if ( !is_wp_error($taxonomy) ) {
				if ( $field == 'title' ) {
					$f = &$r->title;
				} else {
					$f = &$r->content;
				}
				if ( $position == 'before' ) {
					$f = $taxonomy->labels->name  . $delimiter . $f;
				} else {
					$f .= $delimiter . $taxonomy->labels->name;
				}
				}
			}
	}
	return $results;
}

Variable to change in the code

  • $position (line 4) - 'before' or 'after', where you need to display the taxonomy name

  • $field (line 5) - The field name, 'title' or 'content'

  • $delimiter (line 6) - String, which is placed between the taxonomy name and the field

Last updated

Copyright Ernest Marcinko