Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Add “Showing (X Number) search results for: (searched word)”

  • Hi, I need a little help
    Subject:
    Small aesthetic customization of search results page using Query Loop / Loop Template and a Layout to remove sidebar. Everything went well although I didn’t manage to add some things.

    Here you can see:
    https://jrmora.com/?s=test

    Context in this thread:
    https://generate.support/topic/first-steps-to-create-custom-search-results-page/

    Now I need a help to add in the search loop with a hook Element the sentence “Showing results (X Number) for: ( searched word)” because I can’t find the correct syntax in search.php (I understand it’s called from another site).

    Regards

  • Hi there,

    it will require some PHP.

    1. Add this PHP snippet to your site:

    add_filter( 'render_block', function( $block_content, $block ) {
    
    if ( !is_admin() && is_search()
        && ! empty( $block['attrs']['className'] ) 
        && strpos( $block['attrs']['className'], 'search-title' ) !== false
    ) {
        global $wp_query;
        $found_posts = $wp_query->found_posts;
        if ( $found_posts ) {
    
            if ( $found_posts > 1 ) {
                $block_content = 
                sprintf(
                __( 'Showing %1$s Results for: "%2$s"', 'generatepress' ),
                $found_posts,
                '<span>' . get_search_query() . '</span>'
                );
            } elseif ( $found_posts === 1 ) {
                $block_content = 
                sprintf(    
                __( 'Showing 1 Result for: "%1$s"', 'generatepress' ),
                '<span>' . get_search_query() . '</span>'
                );
            }
    
        }
    
    }
    
    return $block_content;
    
    }, 10, 2 );
    

    2. in your Element add a Headline block with some static text for you search title and give it a CSS Class of: search-title

  • Hi, David.

    Great! It works just as it should. Appreciate it.

    Now I’m going to keep fighting with the reading time and the hit counter.

    Thank you very much.

  • You’re welcome

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