- This topic has 9 replies, 3 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
December 10, 2022 at 10:59 am #2457276
Tim Taricco
I recently received support help in regards to a default “no posts found” message when no posts match the query when using the GenerateBlocks Query Loops block: https://wordpress.org/support/topic/query-loop-block-default-output-when-no-posts/#post-16273656. This solution feels like a bandaid, but it works. The GB Query Loop block should have a field for a default “no posts found” message.
In a related issue, when using the Page Hero element type in an archive template and the GB Headline block with dynamic content (Title), if there are no posts returned in the query, the dynamic content in the GB Headline block doesn’t render = the Title is blank.
In an ideal world, the dynamic text (Title) should render in the GB Headline block, and the “no posts found” message would show.
December 11, 2022 at 8:21 am #2457976David
StaffCustomer SupportHi there,
we are looking at ways to handle this without custom code or adding proprietary fields and it is high on our list of to dos.
Do you need assistance today for making changes to the
no resultsarchive ?December 12, 2022 at 8:42 am #2459242Tim Taricco
If you have a solution to the Archive template dynamic text Title being empty when the post query has no results, that would be great.
December 12, 2022 at 9:40 am #2459349David
StaffCustomer SupportCan you confirm are you using the GP Dynamic Data ( set in the Block Toobar ) ?
December 12, 2022 at 10:30 am #2459431Tim Taricco
Yes, “Title” is selected in the GP Dynamic Data section of the toolbar.
December 12, 2022 at 6:21 pm #2459757Fernando Customer Support
Hi Tim,
When there are no posts in an archive page, it uses the
no-results.phptemplate by default.To add a “No Posts Found” title, you can add another Headline Block for the “No Posts Found” Text. Then, give it a class of
cu-no-posts-headlinethrough the Advanced section of the Block Settings. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/After doing so, add this PHP:
add_filter( 'render_block', function( $block_content, $block ) { if (!is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'cu-no-posts-headline' ) !== false && have_posts() ) { return ''; } return $block_content; }, 10, 2 );Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
This Snippet removes the “No Posts Found” title on Archive pages with Posts and shows it if there aren’t any posts.
December 13, 2022 at 9:15 am #2460694Tim Taricco
Hi Fernando, unfortunately, your solution doesn’t address dynamic content because the Headline block should display the category name for the specific Archive. What I have done is created a Page Hero Element and would like the Category name to show in the Headline block even when there are no query results for that category. The manual solution would be to create a separate Page Hero Element for each category, but that’s not a good solution. Anyway, that’s the reason for my support request.
December 14, 2022 at 8:13 am #2461791David
StaffCustomer SupportOk, so here is a work around.
1. Add this PHP Snippet to your site:
add_filter( 'render_block', function( $block_content, $block ) { $class = 'no-results-title'; // Additional CSS Class on block to find $string = 'no results'; // string of text to replace if ( !is_admin() && ! empty( $block['attrs']['className'] ) && $class === $block['attrs']['className'] ) { if ( !have_posts() && is_archive() ){ $replace = get_the_archive_title(); $block_content = str_replace( $string , $replace , $block_content ); } else { $block_content = null; // if there are posts remove it title } } return $block_content; }, 10, 2 );2. In your block element add a second headline block.
Don’t set it to dynamic.
Just add some static text that includes the:no results
Give it an Advanced > Additional CSS class of:no-results-titleThis title will only be displayed when viewing an empty archive, and will show the Term name in its place.
December 14, 2022 at 10:05 am #2461908Tim Taricco
Thank you. This worked perfectly.
December 15, 2022 at 4:10 am #2462626David
StaffCustomer SupportAwesome – glad to be of help
-
AuthorPosts
- You must be logged in to reply to this topic.