Archived topic
How to modift the search results Title?
4 replies · Started by Halil on March 19, 2023
Hi,
I created a loop template to show on category archives and search results successfull. I've used a query loop in that template with "Inherit Query from Template" option checked.
I also created a page hero to customize the title of post category archive, which works fine.
However I couldnt find a way to customize search results's title! How do I get the name of searched text so shat I can use it in page hero to show on search results?
Hi Halil,
You can use the below method:
1. In your page hero, add a CSS class to the headline block which represents the search title, eg. search-query:
https://www.screencast.com/t/aqbOVSpQ3s
2. In the headline block, please include the search query text, so we can use the below PHP to replace this text with the dynamic search query text, please refer to the screenshot in No.1.
3. Add the PHP code below:
add_filter( 'render_block', function( $block_content, $block ) {
if ( ! empty( $block['attrs']['className'] ) && 'search-query' === $block['attrs']['className'] && is_search() ) {
$search_query = get_search_query();
$block_content = str_replace('search query', $search_query, $block_content);
}
return $block_content;
}, 10, 2 );
Adding PHP: https://docs.generatepress.com/article/adding-php/
thanks Ying, it works but now I have 2 search results title on the page,
I checked "disable title" and "disable primary post meta" on the page hero element.
how can I remove the original one?
pls see the pic
https://ibb.co/hgMmNNR
nevermind Ying, activating loop template removed the duplicate title. thank you, you guys rock always!
Glad to hear that :)