Posted on

Category title in the search results page

If you’re using category restriction dropdown on your search form, here’s a bit of code that you can add to your search results template to show how many hits were found and what the category is. If no category was selected, this’ll just show how many hits were found.

    $cat = get_cat_name($wp_query->query_vars['cat']);
    $s = $wp_query->query_vars['s'];
    echo $wp_query->found_posts . " results found with ' $s'";
    if ($cat != '') {
        echo " in the '$cat' category";
  • Way late to the party, but just found this post and the above code worked great for me. Thanks!