[Resolved] How to modift the search results Title?

Home Forums Support [Resolved] How to modift the search results Title?

Home Forums Support How to modift the search results Title?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #2573126
    Halil

    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?

    #2573286
    Ying
    Staff
    Customer Support

    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/

    #2573353
    Halil

    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

    #2573355
    Halil

    nevermind Ying, activating loop template removed the duplicate title. thank you, you guys rock always!

    #2574463
    Ying
    Staff
    Customer Support

    Glad to hear that 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.