Archived topic
Search bar assigns a title to the page
9 replies · Started by Harsha Kiran Gudibandi on February 28, 2023
When you search for a term and the results show, the search page gets assigned a title using the first result.
How do I fix this?
Hi there,
Can you share a link to your site ?
https://www.adakta.co/?s=stats
Here's an example.
OK, so the site uses a Block Element titled in Appearance > Elements titled: Global Page Header
And that was neveer meant for search results.
To make it work for them:
1. Add this PHP Snippet to your site:
add_shortcode('multi_title', 'get_custom_title' );
function get_custom_title() {
if ( is_singular() ) {
return get_the_title();
} elseif ( is_search() ) {
return 'Search results for: ' . get_search_query();
} elseif ( is_archive() ) {
return get_the_archive_title();
} else {
return get_bloginfo( 'name' );
}
}
2. edited the Global Page Header Element.
2.1 Select the Headline showing the title and uncheck the Dynamic Data option in the toolbar.
2.2 replace the title text with this shortcode [multi_title]
3. Publish the changes
Hello again. Regarding step #1, where should I add the PHP snippet?
Oh sorry, this doc explains how to add PHP:
Hey David. Thanks for the help. It's changed now. However, I have one more thing, https://www.adakta.co/?s=stats it's not double. How do I remove one instance of it?
Try adding this PHP snippet as well to remove the original H1:
add_action('wp',function() {
remove_action( 'generate_before_loop', 'generate_do_search_results_title' );
});
Perfect, thanks both
No Problem!