Site logo

Archived topic

Search results page element

5 replies · Started by Dan on November 2, 2022

Viewing posts 1–6 of 6

Hi
I created a page hero element that will show up on search results page.
I have no option to display: "Search result for: --The search query--"
I saw this topic: https://generatepress.com/forums/topic/header-element-not-working-for-search-results-page/

using this filter:

add_filter( 'generate_page_hero_post_title', function( $post_title ){
	if(is_search()){
		$post_title = __( 'Search Results for: %s', 'generatepress' ).'<span>' . get_search_query() . '</span>';
	}
	
	return $post_title;
},12,1 );

Is that the only way?
Does GP have a dynamic text for displaying the search query?

You can see it here: https://noonaesthetics.ca/?s=multi

Thanks,
Dan

Hi Dan,

Here's the other way.

Create a Headline Block. Enable dynamic data in the tool bar settings of the Block. Then, set the dynamic text type to title. Example: https://share.getcloudapp.com/P8u742ep

Add my-search-title to the class list of the Headline Block. Lastly, add this PHP snippet:

add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
    if ( !is_admin() && is_search() && ! empty( $block['attrs']['className'] ) && 'my-search-title' === $block['attrs']['className'] ) {
        $text = get_search_query();
    }

    return $text;
}, 10, 2 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Thanks Fernando.
I've applied you instructions and it's working as expected:
https://noonaesthetics.ca/?s=igloo

But I think it kinda defeats the purpose when using dynamic GP headlines to introduce custom php.
I would think that if we want to use a heading in the search results template, GP would have an option to use key items that are used in that type of template - for example - displaying the search query.

Dan

You're welcome Dan! Though it's not conventionally possible, this specific filter we used - generate_dynamic_element_text - is meant for situations like this. It specifically for changes needed with regards to Dynamic Headline Blocks if it's not possible through the available settings. I agree that an option for the Search query as a dynamic source would be ideal, but, using the available filter as opposed to adding extra code plugin-wise may be more beneficial to most users performance-wise.

You may raise a feature request here though so our developers will see if many of our customers would see such an option optimal: https://community.generateblocks.com/c/feature-requests/6

Hi Dan,

FYI - the filter provided here by Fernando - is for the GP Dynamic Data options not GenerateBlocks.

At this time customising the search title using any method requires some code ( including the core Search Results Title block ).

Ideally we would need to add a specific dynamic data functions for:

1. get_search_query()
2. $wp_query->found_posts

It is something we are looking at but it gets complicated real quick:

For now i would:

1. Use a GB Headline block and give it an Advanced > Additional CSS Class eg. search-title
2. Use the render_block filter to target that class and replace the block content with your desired string eg.


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(
                __( '%1$s Results for: "%2$s"', 'generatepress' ),
                $found_posts,
                '<span>' . get_search_query() . '</span>'
                );
            } elseif ( $found_posts === 1 ) {
                $block_content = 
                sprintf(    
                __( '1 Result for: "%1$s"', 'generatepress' ),
                '<span>' . get_search_query() . '</span>'
                );
            }

        }

    }

    return $block_content;

}, 10, 2 );

I added in the conditional to switch from results to result based on found post count.
Which is one of the hurdles when trying to build this into a UI

This archived topic is closed to new replies.