Site logo

[Support request] How to “hide” GenerateBlock when related custom-meta field is empty?

Home Forums Support [Support request] How to “hide” GenerateBlock when related custom-meta field is empty?

Home Forums Support How to “hide” GenerateBlock when related custom-meta field is empty?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2429653
    Sascha

    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

    #2429872
    Ying
    Staff
    Customer Support

    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 );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.