Archived topic
How to "hide" GenerateBlock when related custom-meta field is empty?
1 reply · Started by Sascha on November 23, 2022
Hello,
please, allow me to handle this request here, since I use both GPP & GBP and would like to keep all my support tickets here.
When I create a template for a Custom Post Type, that has a few Custom Fields, I know, that empty data will not be displayed.
But sometimes there will be additional headline-field, which need to be hidden too. Example:
1. Headline-Block "Location-Title" (h4, plain)
2. Headline-Block "Location-Data" (h4, dynamic > display data from "location_data" field in related post)
Is there a way to hide this 1. Headline-Block too, when the related field for 2. HB is empty?
Thank you in advance and sorry for my low english,
Sascha
1. Add an addtional CSS class to the NO.1 Headline block, eg. location-title.
2. Add this PHP snippet,
add_filter( 'render_block', function( $block_content, $block ) {
$location_data = get_post_meta( get_the_ID(), 'location_data', true );
if ( ! empty( $block['attrs']['className'] ) && 'location-title' === $block['attrs']['className'] && empty($location_data )) {
$block_content = '';
}
return $block_content;
}, 10, 2 );