[Resolved] Conditional Elements and GP 3.0

Home Forums Support [Resolved] Conditional Elements and GP 3.0

Home Forums Support Conditional Elements and GP 3.0

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1391193
    Samuel

    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.

    #1391251
    David
    Staff
    Customer Support

    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?

    #1391442
    Samuel

    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?

    #1391638
    David
    Staff
    Customer Support

    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 );
    #1391684
    Samuel

    Perfect. Thank you so much

    #1391770
    David
    Staff
    Customer Support

    You’re welcome

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