Site logo

[Support request] Modify search title

Home Forums Support [Support request] Modify search title

Home Forums Support Modify search title

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2077608
    Aaron

    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?

    #2077758
    David
    Staff
    Customer Support

    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.

    #2077773
    Aaron

    I would like to output "%s" was found on these locations: I can’t do that with str_replace, eh?

    #2077914
    David
    Staff
    Customer Support

    In that case you would need to use the entire filter – seen here:

    https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/structure/archives.php#L129

    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;
    });
    #2077927
    Aaron

    Thank you, David, have a nice day!

    #2078116
    David
    Staff
    Customer Support

    You’re welcome and you too!

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