Archived topic
Modify search title
5 replies · Started by Aaron on January 12, 2022
I would like to change the search results title from Search Results for: %s to something different. I did a child theme with a modified inc/structure/archives.php but it did not change anything. Which files from generatepress do I need in my child in this approach?
Hi there,
you can use the generate_search_title_output filter hook like so
function custom_search_result_title( $items) {
if ( is_search() ) {
$items = str_replace( "Search Results for", "Your replacement search text", $items );
}
return $items;
}
add_filter( 'generate_search_title_output', 'custom_search_result_title', 9999, 2 );
Change the Your replacement search text to suit your needs.
I would like to output "%s" was found on these locations: I can't do that with str_replace, eh?
In that case you would need to use the entire filter - seen here:
Something like this:
add_filter('generate_search_title_output',function($title) {
$title = sprintf(
'<header %s><h1 class="page-title">%s</h1></header>',
generate_get_attr( 'page-header' ),
sprintf(
/* translators: 1: Search query name */
__( '%s was found on these locations:', 'generatepress' ),
'<span>' . get_search_query() . '</span>'
)
)
return $title;
});
Thank you, David, have a nice day!
You're welcome and you too!