This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Taxonomic labels on category and tag search results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Taxonomic labels on category and tag search results

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #35719
    aharoniumaharonium
    Participant

    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!

    #35730
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    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.

    #35747
    aharoniumaharonium
    Participant

    And 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"; }

    #35757
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Not 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;
    }
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.