Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Taxonomic labels on category and tag search results
- This topic has 3 replies, 2 voices, and was last updated 4 years, 6 months ago by
Ernest Marcinko.
-
AuthorPosts
-
November 24, 2021 at 10:43 pm #35719
aharonium
ParticipantCurrently, 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 #35730Ernest Marcinko
KeymasterHi,
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.
November 26, 2021 at 3:46 am #35747aharonium
ParticipantAnd 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 #35757Ernest Marcinko
KeymasterNot exactly, more like:
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; } -
AuthorPosts
- You must be logged in to reply to this topic.