Fallback content for Dynamic Tag

  • Is there a way to set “fallback content” for a Dynamic Tag? For example, using Post Meta for an ACF field, if that ACF field is empty, rather than hiding the field by default, the “fallback content” would show. A good use case example would be having a “custom-cta” field for the text of a button that falls back to “Learn More” if a custom CTA is not in the field.

  • Hi Tim,

    There isn’t a built-in fallback option for dynamic tags yet, but you can add one with the generateblocks_dynamic_tag_output filter. This PHP snippet returns “Learn More” whenever the custom-cta field is empty:

    add_filter( 'generateblocks_dynamic_tag_output', function( $output, $options ) {
        $tag_name = $options['tag_name'] ?? '';
        $key      = $options['key'] ?? '';
        if ( 'post_meta' === $tag_name && 'custom-cta' === $key && '' === trim( wp_strip_all_tags( $output ) ) ) {
            $output = 'Learn More';
        }
        return $output;
    }, 10, 2 );

    Let me know if this helps!

  • Thanks @Ying. That solution works. I like how you said “yet” in your response. A fallback option definitely seems like a good feature for GenerateBlocks Dynamic Tags.

  • You are welcome   🙂

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