- This topic has 5 replies, 3 voices, and was last updated 1 year, 4 months ago by David.
-
AuthorPosts
-
November 2, 2022 at 3:12 pm #2399073Dan
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,
DanNovember 2, 2022 at 6:11 pm #2399165Fernando Customer SupportHi 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
November 3, 2022 at 10:18 am #2400414DanThanks Fernando.
I’ve applied you instructions and it’s working as expected:
https://noonaesthetics.ca/?s=iglooBut 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
November 3, 2022 at 5:10 pm #2400756Fernando Customer SupportYou’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
November 4, 2022 at 7:29 am #2401500DanThanks Fernando.
I added it to the feature requests category – https://community.generateblocks.com/c/feature-requests/6
Another feature to add is displaying the # of results. is that doable too?Thanks
DanNovember 4, 2022 at 8:52 am #2401791DavidStaffCustomer SupportHi 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 therender_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
toresult
based on found post count.
Which is one of the hurdles when trying to build this into a UI -
AuthorPosts
- You must be logged in to reply to this topic.