Site logo

Archived topic

Conditional Blocks in Block Element

3 replies · Started by Liam on June 20, 2021

Viewing posts 1–4 of 4

Hi there,

I'm currently converting one of my sites to Block Elements. I've created a Content Template Block Element for my archives, and my question is the following: Is it possible to have conditional (Generate)Blocks in that template? The site is a food blog, and I'd like to display icons/text for vegetarian, low-card, etc. I know I can create templates for individual categories, but to get all combinations covered. The diet terms are a custom taxonomy (a vegetarian meal gets assigned the "vegetarian" category in the taxonomy "diet" etc.).

Thanks for any help!
Liam

Hi there,

you can use the Dynamic Text type - List Terms and select your custom taxonomy. If there are no Terms for a specific post that field will not be displayed.

Hi David

Thanks for the reply. I think I should have given a bit more information. My question is whether there is something like an "if/then" function for individual blocks. E.g. if the recipe is vegetarian, then the button block with a plant icon will be displayed and if not, then not.

Hi Liam,

It's going to be real tricky to do but it may be doable with generate_dynamic_element_text filter:

Not sure if this is 100% accurate but you can play around with this example:

add_filter( 'generate_dynamic_element_text', function( $custom_field, $block ){
    if ( 'term-meta' === $block['attrs']['gpDynamicTextCustomField'] ) {
        if(your condition here){
	        $custom_field = '';
        }
    }
    return $post_date;
},15,2);
This archived topic is closed to new replies.