Displaying taxonomy name in taxonomy term results
This custom code is to display the taxonomy name along with the term results.
1
add_filter('asp_results', 'asp_display_tax_name_in_results');
2
function asp_display_tax_name_in_results($results) {
3
// Change these variables according to the comments after
4
$position = 'before'; // 'before' or 'after'
5
$field = 'title'; // 'title' or 'content'
6
$delimiter = ' - '; // Characters between the taxonomy name and the field
7
8
// --- DO NOT CHANGER ANYTHING BELOW ---
9
foreach($results as $k=>&$r){
10
if ( $r->content_type == 'term' ) {
11
$taxonomy = get_taxonomy( $r->taxonomy );
12
if ( !is_wp_error($taxonomy) ) {
13
if ( $field == 'title' ) {
14
$f = &$r->title;
15
} else {
16
$f = &$r->content;
17
}
18
if ( $position == 'before' ) {
19
$f = $taxonomy->labels->name . $delimiter . $f;
20
} else {
21
$f .= $delimiter . $taxonomy->labels->name;
22
}
23
}
24
}
25
}
26
return $results;
27
}
Copied!

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
Copy link
Contents