Site logo

Archived topic

Conditional Elements and GP 3.0

5 replies · Started by Samuel on August 7, 2020

Viewing posts 1–6 of 6

Quick question--Is there a way to make an element conditional when the page loads. For example, if I have an element that adds content to a post is there a way to determine when the page loads if the element actually shows via a filter or something similar?

Also is testing open for 3.0? Just asking because I'm working on a big project and would love to have it ready for the next version of GP.

Hi there,

GP 3.0 we're still developing but its next on our list of updates so watch this space.

I am not sure what you mean by the conditional display...
Is it case of IF Element === display then Do something else?

So for example, if I had an element configured to show on a single post page. However, I only wanted that element to show if a certain meta value was set for that post. Is there a filter or something similar that could be used to check that meta and then cause the element not to load if the meta value is not set?

You can overwrite the display rules for a Hook using the generate_hook_element_display filter. There also ones for _layout , _header and _block

https://docs.generatepress.com/article/generate_hook_element_display/

Heres an example to display a hook if the meta field is NOT empty:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {

    // Get the Meta value for condition
    $conditionalMeta  = get_post_meta( get_the_ID(), 'my_conditional_meta_key', true );

    // If not empty display then display Element id 10

    if ( 10 === $element_id && !empty($conditionalMeta) ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

Perfect. Thank you so much

You're welcome

This archived topic is closed to new replies.