Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Remove container with no “Updated Date”

  • I have a container, inside is a label (Headline) and another headline field that contains dynamic data (Post date – updated).

    I want to hide this container if there is no “updated date”. But in order to use the REMOVE CONTAINER CONDITION in the block settings, I need to know the META FIELD NAME.

    Does anyone know the meta field name for the updated date? Or if there is a document that lists all the meta field names so i dont need to ask again in the future.

    Thanks
    Chris

  • Hi there,

    the dates are not saved in Post Meta, so there is no field to check for.
    Instead you could use a PHP Snippet with the render_block hook to remove a Container Block with a specific class if there is a) no modified date or b) the modified date is the same as the published date.

    Here is the PHP Snippet:

    
    add_filter( 'render_block_generateblocks/container', function( $block_content, $block ) {
        if ( 
            ! is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'post-updated' ) !== false 
        ) {
            $post_modified_date = get_the_modified_date();
            $post_published_date = get_the_date();
            if ( ! ( $post_modified_date && $post_modified_date !== $post_published_date ) ) {
                $block_content = '';
            }
        }
    
        return $block_content;
    }, 10, 2 );
    
    

    Then select the Container Block, in Advanced > Additional CSS Classes add: updated-date-container

  • Thanks David, thats great!

  • Could be me but it seems to be doing the opposite. Removing the block if it DOES have a date. Keeping it if it doesnt.

  • Oh my – having a logic overload today. I updated the code above, give that a shot.

  • We all have those days lol.

    Page doesnt seem to want to load using the new one. Just get a blank screen until I remove the Additional CSS Class.

    I tried clearing my browser cache and also purged my hosting cache.

    EDIT: Managed to sort it. It was the is_empty

    
    add_filter( 'render_block_generateblocks/container', function( $block_content, $block ) {
        global $post;
        
        if ( 
            ! is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'updated-date-container' ) !== false 
        ) {
            $post_modified_date = get_the_modified_date( '', $post->ID );
            $post_published_date = get_the_date( '', $post->ID );
            
            if ( empty( $post_modified_date ) || $post_modified_date === $post_published_date ) {
                $block_content = '';
            }
        }
    
        return $block_content;
    }, 10, 2 );
    
  • Right thats it – i am going back to bed lol
    I did just correct the code above in case… but i am glad to hear you got it working!

  • Hahaha we got there in the end!

    Thanks again.

  • You’re welcome 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.