-
YEAR93
Hello,
Is it possible to display the search results count? For example -> 970 search results. If so, How?
-
Fernando
Hi there,
For reference, could you share the link to your site? I’ll check how your Search page is setup and see what code is needed.
-
YEAR93
View below
-
Fernando
Try adding this snippet:
function custom_search_result_title( $items) { if ( is_search() ) { global $wp_query; $found_posts = $wp_query->found_posts; $items = $items . '<h3 class="found-posts-count">' . $found_posts . ' Results</h3>'; } return $items; } add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
YEAR93
Is it possible to make a shortcode of this snippet or can you explain how I add this in front of the search results page title? The title is now “Zoekresultaten voor: … ” but I want this to be “23 zoekresultaten voor: … “.
-
Use function below to create shortcode
[custom_search_result_title]
:function custom_search_result_title_shortcode( $atts ) { ob_start(); if ( is_search() ) { global $wp_query; $found_posts = $wp_query->found_posts; echo '<h3 class="found-posts-count">' . $found_posts . ' Results</h3>'; } $output = ob_get_clean(); return $output; } add_shortcode( 'custom_search_result_title', 'custom_search_result_title_shortcode' );
-
YEAR93
Thank you, and where can I add this? Because it looks like the title on my search results pages isn’t from my custom content block but maybe from an theme template file?
-
Thank you, and where can I add this
What do you mean?
-
YEAR93
View screenshot 1 displays the current appearance of the search results page title, while screenshot 2 illustrates what I want to achieve.
-
You don’t want it to show the 3?
-
YEAR93
I want the count of the search results to be displayed in the page title of the search results page.
-
Fernando
I see. It won’t be a Shortcode though. Try this code instead:
function custom_search_result_title( $items) { if ( is_search() ) { global $wp_query; $found_posts = $wp_query->found_posts; $myreplace = '</h1>'; $myinsert = '<span class="found-posts-count"> ' . $found_posts . ' Results</span></h1>'; $items = str_replace( $myreplace, $myinsert , $items ); } return $items; } add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 );
-
YEAR93
Works, but that snippet isn’t adding the count inside the page title.
-
Fernando
Could you reactivate the code? I’ll check what’s going on.
-
YEAR93
Okay, I did now.
-
Fernando
I see. I updated the code I just shared above. Can you try that instead?
- You must be logged in to reply to this topic.