- This topic has 5 replies, 3 voices, and was last updated 3 years, 4 months ago by
Fernando.
-
AuthorPosts
-
November 14, 2022 at 8:28 am #2415154
Sam
Hi,
I’ve looked through the code around how the flag gpRemoveContainerCondition in the $block[‘attrs’] goes about removing a container where there is no post meta, and was looking to filter/extend this in the same way that the filters like generate_dynamic_element_XXX modify the text.
My use case is that I have a Generate Blocks headline displaying the term meta on an element of a taxonomy term page, and I’d like to hide the container it’s in if the term meta isn’t present.
Is there any way to achieve this through a filter of the block that I’ve not noticed?
Thanks,
SamNovember 14, 2022 at 9:05 am #2415200David
StaffCustomer SupportHi there,
you could use the core
render_blockfilter to remove the $block_content for example:add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && is_category() && ! empty( $block['attrs']['className'] ) && 'your-custom-class' === $block['attrs']['className'] ) { $block_content = null; } return $block_content; }, 10, 2 );November 14, 2022 at 2:30 pm #2415601Sam
Hi David,
Thanks – that looks really promising and like it should achieve what I need.
I’ve just tried it, and it looks like the conditional is evaluating false on is_category() (I have removed the two last conditions to see if it was an issue with class name). The element’s display rule is set to render on a custom taxonomy archive that is rendering a WooCommerce template – would this have an effect on this working?
I can’t remove the is_category() check without triggering a critical error, so am not sure where to go from here to diagnose.
For what it’s worth, I also noticed that GenerateBlocks is having an issue in the element on the archive page – it’s not rendering a headline with dynamic content where there are no posts in the loop of the archive page – not sure if it’s related to this issue, but just thought I’d mention as that’s two issues on the same archive.
Thanks in advance for any light you can shed – and sorry for coming back to you on what should have been an easy answer!
November 14, 2022 at 5:45 pm #2415733Fernando Customer Support
Hi Sam,
render_block()should work anywhere even on Custom Taxonomy archive pages. Try replacingis_category()withis_tax().David’s code is just an example of its usage. It needs modifications for it to work as you would prefer.
Can you provide admin login credentials so we can have a better grasp of the concept?
Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
November 15, 2022 at 12:31 am #2415967Sam
Ah, all sorted now – thanks for coming back to me. Turns out it needs to return a blank string rather than null or else a core function trips out:
[15-Nov-2022 07:16:24 UTC] PHP Fatal error: Uncaught TypeError: Argument 1 passed to Automattic\WooCommerce\Blocks\BlockTypes\ClassicTemplate::add_alignment_class_to_wrapper() must be of the type string, null given, called in /home/wp-includes/class-wp-hook.php on line 310 and defined in /home/wp-content/plugins/woocommerce/packages/woocommerce-blocks/src/BlockTypes/ClassicTemplate.php:233Still need to figure out the weird GB dynamic data thing, but looks like that’s now not for this thread.
Thanks for your help with this.
November 15, 2022 at 12:36 am #2415971Fernando Customer Support
I see. You’re welcome Sam! Feel free to raise a new topic if you have any further issues.
-
AuthorPosts
- You must be logged in to reply to this topic.