Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Taxonomic labels on category and tag search results
This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 3 months ago.
- AuthorPosts
- November 24, 2021 at 10:43 pm #35719
Currently, our search results include matching category and tag. This is great except we would prefer if the results returned also indicated with their taxonomy.
For example, a search for “hanukkah” returns:
“Hanukkah (12)”
“Hanukkah readings (10)”
“Hanukkah recipes (5)”We would prefer that the taxonomy for the results were labeled as such.
“category: Hanukkah (12)”
“category: Hanukkah readings (10)”
“tag: Hanukkah recipes (5)”If this is an option anywhere that would be excellent. We searched through the settings and documentation for any hint as to how to achieve this, but we apologize in advance if we missed anything obvious!
November 25, 2021 at 10:31 am #35730Hi,
I’m afraid there is no option for that, but it could be doable via some minor custom code.
add_filter('asp_results', 'asp_display_tax_name_in_results'); function asp_display_tax_name_in_results($results) { foreach($results as $k=>&$r){ if ( $r->content_type == 'term' ) { $taxonomy = get_taxonomy( $r->taxonomy ); if ( !is_wp_error($taxonomy) ) { $r->title = $taxonomy->labels->name . ": " . $r->title; } } } return $results; }
Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
November 26, 2021 at 3:46 am #35747And to change “Categories” to “category” and “Tags” to “tag” would I do something like the following?
if ($r->content_type == 'Categories') { $term = "category"; } else { $term = "tag"; }
November 26, 2021 at 1:08 pm #35757Not exactly, more like:
Best,add_filter('asp_results', 'asp_display_tax_name_in_results'); function asp_display_tax_name_in_results($results) { foreach($results as $k=>&$r){ if ( $r->content_type == 'term' ) { $taxonomy = get_taxonomy( $r->taxonomy ); if ( !is_wp_error($taxonomy) ) { switch ($taxonomy->labels->name) { case "Categories": $name = "category"; break; case "Tags": $name = "tag"; break; default: $name = $taxonomy->labels->name; } $r->title = $name . ": " . $r->title; } } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.