How to show cats title in search result?

Forums › Forums › Search & Filter Pro › How to show cats title in search result?

Viewing 10 posts – 1 through 10 (of 15 total)
  • Anonymous
    #10925

    The following shows the term i search as a title title if I use the search field but if I select a category from the drop down, the results are fine, the number of results displayed in the title are fine but i get an empty title:

    		<h1 class="search-title">
    			<?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"
    		</h1>

    Becomes:

    6 Search Results Found For: ""

    Where between For: “..” there should be the category or categories and the search term if all have been used to make the search i suppose

    Thanks

    Anonymous
    #10978

    Any idea?

    Anonymous
    #13501

    Hi Roberto
    i have the same problem – did you get a solution for this ?

    Ross Moderator
    #13652

    Hey Roberto

    This must have slipped through – thanks Josef for reviving it.

    To get your search term then please read the below:

    If you are using the shortcode to display results, then you can use:

    <?php echo esc_html($query->query['s']); ?>

    When using the archive method and using a template from your theme,

    <?php the_search_query(); ?>

    Should be absolutely fine, however if this is not working for some reason, you could try:

    <?php echo esc_html($wp_query->query['s']); ?>

    Also, remember to check that ['s'] is set before using it.

    Josef, I’ve noticed your other ticket too, about extracting the rest of the search info such as taxonomy terms etc – I’ll follow up shortly with a post about that.

    Thanks

    Ross Moderator
    #13653

    Ok here is some sample code to retrieve the values for a specfic taxonomy:

    function get_taxonomy_values($taxonomy_key)
    {
    	global $wp_query;
    	global $wpdb;
    	
    	if(isset($wp_query->query[$taxonomy_key]))
    	{
    		if (strpos(esc_attr($wp_query->query[$taxonomy_key]),',') !== false)
    		{
    			$ochar = ",";
    			$taxterms = explode($ochar, esc_attr(($wp_query->query[$taxonomy_key])));
    			
    			$taxlabels = array();
    			foreach ($taxterms as $taxterm)
    			{
    				$taxlabel = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE slug='".$taxterm."'");
    				array_push($taxlabels, $taxlabel);
    			}
    			
    			return implode(" or ", $taxlabels);
    		}
    		else
    		{
    			$ochar = "+";
    			$taxterms = explode($ochar, esc_attr(urlencode($wp_query->query[$taxonomy_key])));
    			
    			$taxlabels = array();
    			foreach ($taxterms as $taxterm)
    			{
    				$taxlabel = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE slug='".$taxterm."'");
    				array_push($taxlabels, $taxlabel);
    			}
    			
    			return implode(" or ", $taxlabels);
    		}
    	}
    }

    This is just something I’ve written myself, you’ll want to modify it to display how you want..

    Then to display just do:

    echo get_taxonomy_values("_sft_pa_size");

    Where the taxonomy name is pa_size – if you use this plugin without Ajax, you will notice the URL update when you search, if in this example I had a taxonomy pa_size and submitted the form then you will see _sft_pa_size in the URL, this is what you need to use to retrieve a specific taxonomy.

    Categories and tags are also treated as taxonomies, their names would be _sft_category and _sft_post_tag respectively.

    Thanks

    Anonymous
    #13696

    I can’t seem to get the search terms to display either. I’ve tried as suggested without luck. I’m using the shortcodes to display the results. Is there a specific location they should be placed?

    Ross Moderator
    #13709

    Which part is giving you a problem, the actual search terms a user can enter in a search box?

    The example above should work fine:

    <?php echo esc_html($query->query['s']); ?>

    Just make sure its after has_posts

    Thanks

    Anonymous
    #13787

    Hi Roos,
    Thanks
    It works for me but only in the first search, when i do another search the results query is update normally but the title not change ‘get_taxonomy_values’

    Ross Moderator
    #13791

    Hi Josef

    If you are using ajax, and the text is not updating when you submit, then you must place the title in your results template – within the area that is getting loaded by ajax – same goes for your pagination.

    Thanks

    Anonymous
    #13798

    Hi Ross,
    I have tested number of places before replaying your answer, and even include it in the loop the search-filter folder.

    lets make sure that i did the process correct.
    1. insert the code to the theme functions.php
    2. include the “echo get taxonomy value” in the search-filter > id.php {when the id is the id of the search as in the shortcode number }

    Hope for your help
    thanks in advance
    Josef

Viewing 10 posts – 1 through 10 (of 15 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.