Archives

Home Forums Search Search Results for 'generate_element_display'

Search Results for 'generate_element_display'

Home Forums Search Search Results for 'generate_element_display'

Viewing 15 results - 1 through 15 (of 162 total)
  • Author
    Search Results
  • Ying
    Staff
    Customer Support

    Hi Max,

    Try this:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
    	global $post;
    	$child_pages = get_pages(
        array(
    	'child_of' => 67890,
    	   )
        );
        $child_pages_ids = wp_list_pluck( $child_pages, 'ID' );
    
        if ( 12345 === $element_id && ( is_page($child_pages_ids)) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    #2350545
    Max

    Hi there!

    I have an element that I want to apply to all children of a page.

    I managed that by creating a snippet:

    `add_filter( ‘generate_element_display’, function( $display, $element_id ) {
    global $post;

    // Adds header-without-menu to all children of [Checkout]
    if ( 12345 === $element_id && ( is_page() && $post->post_parent == ‘67890’ ) ) {
    $display = true;

    This works great, but I noticed that children of those children pages don’t have the element.

    Is there a way to modify this snippet so that the element also shows on all the children of children?

    Thanks!

    Fernando
    Customer Support

    Great!

    As for the other inquiry, you’ll need PHP for that. Specifically, the generate_element_display filter is what you’ll need to use: https://docs.generatepress.com/article/generate_element_display/

    So, basically, you’ll need something like this for instance:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        if ( 100 === $element_id && is_paged() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    Replace 100 with your Block Element post id.

    #2330307
    David
    Staff
    Customer Support

    Hi there,

    you can use the generate_element_display filter hook to change the display of an element:

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

    And according to this reply it looks like we can use:

    geodir_is_geodir_page() to check if we’re on a geodir page.

    So putting it altogether you can use this snippet:

    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 100 === $element_id && geodir_is_geodir_page() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    You need to change the 100 to match the ID of the Block Element you want to disable.

    #2328355
    David
    Staff
    Customer Support

    Hi there,

    so we want to check if the current post has a category term that is a child of products.
    Currently you would either need to:

    a. Set each of the Post Category -> Child terms in the Element Display Rules

    OR

    b. Use the generate_element_display filter hook to set element display to True if the posts are in child term

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        /// Change 123 below to the ID of the Element we're targeting.
        if ( 123 === (int) $element_id ) {
            $args = array(
                'include'  => get_the_ID(),
                'category' => 456, // Change 456 to the parent category ID we're checking for children in.
                'fields'   => 'ids',
            );
    
            if ( 0 < count( get_posts( $args ) ) ) {
                $display = true;
            }
        }
    
        return $display;
    }, 10, 2 );

    Note the two IDs that need changing

    #2321821
    Fernando
    Customer Support

    Hi Priyankeshu,

    You can try with generate_element_display filter.

    Reference: https://docs.generatepress.com/article/generate_element_display/

    For instance,

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        if ( 100 === $element_id && is_paged() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    Replace 100 with your Block Element’s ID.

    Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

    #2313717
    Tom
    Lead Developer
    Lead Developer

    Caching will definitely mess with anything “random”, as random generation of anything goes against the idea of caching.

    To fix this, let’s go with David’s solution here: https://generatepress.com/forums/topic/random-page-header-on-featured-page-with-text/#post-2309800

    We’re going to tweak the code a little:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        
        // Your arry of element IDs
        $el_array = array( 3137, 3032, 2352, 2523, 2523 );
        
        $rand_element = array_rand( $el_array, 1 );
        
        if ( is_front_page() && $el_array[ $rand_element ] == $element_id ) {
            $display = true;
        }
     
        return $display;
        
    }, 10, 2 );

    That should do the trick I think.

    Let me know 🙂

    David
    Staff
    Customer Support

    Hi there,

    try this snippet:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 10 === $element_id ) {
            if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
                return false;
            }
        }
    
        return $display;
    }, 10, 2 );

    Change the 10 to the ID of the Element.
    This should disable that element wherever Buddypress templates are loaded.

    #2309800
    David
    Staff
    Customer Support

    Ahh – i can see the heroes now – thats cool 🙂
    Odd that occasionally it displays none. Can’t see why it would, theres no global variables being used so its not as if this: unset( $el_array[$rand_element] ); is permanently unsetting values from the array. If that were the case, after all the values had been unset there would be no element shown ever again.

    I wonder if this method would work:

    1. All Header Elements should have NO display rule location set.

    2. Use this snippet instead of the current one:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        
        // Your arry of element IDs
        $el_array = array( 3137, 3032, 2352, 2523, 2523 );
        
        $rand_element = array_rand( $el_array, 1 );
        
        if ( is_front_page() && $rand_element == $element_id ) {
            $display = true;
        }
     
        return $display;
        
    }, 10, 2 );

    This will randomly select one and set its display to True whilst viewing the Front Page.

    #2306578
    David
    Staff
    Customer Support

    OK, thats fine.

    Try this:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        
        // Your arry of element IDs
        $el_array = array( 3137, 3032, 2352, 2523, 2523 );
    
        $rand_element = array_rand( $el_array, 1 );
        unset( $el_array[$rand_element] );
        if ( in_array( (int)$element_id, $el_array ) ) {
            $display = false;
        }
     
        return $display;
        
     }, 10, 2 );

    Update the $el_array array with your IDs.
    And make sure all of the Header Elements are Published and have their display rules set to the same location.

    David
    Staff
    Customer Support

    Hi there,

    for for now, the best method is to create another Block Element to contain your custom No Post Founds message. You can use a Page Hero or any Hook element. Set the Display Rules to match those of your Content Template.

    Make a note of its post ID ( in the Browser URL when editing the post ) and the ID of the content template.

    Then use the following PHP snippet to hide your custom no post element when we have posts, and hide the content template when we don’t have post:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id && have_posts() ) {
            $display = false;
        }
    	
        if ( 456 === $element_id && ! have_posts() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    123 change this to the ID of your custom no post element.
    456 change this to the ID of your Content Template.

    #2298796
    David
    Staff
    Customer Support

    Try this:

     add_filter( 'generate_element_display', function( $display, $element_id ) {
        $your_element_ids = array( 3137, 3032, 2352, 2523, 2523 );
        if (  in_array( (int)$element_id, $your_element_ids ) && is_paged() ) {
             $display = false;
         }
     
         return $display;
     }, 10, 2 );

    Just update the $your_element_ids = array() values

    #2298791
    ER

    It worked, thanks. Can I do like this:

    add_filter( ‘generate_element_display’, function( $display, $element_id ) {
    if ( 3137 === $element_id && is_paged() ) {
    $display = false;
    }

    return $display;
    }, 10, 2 );

    *3137,3032,2352,2523,2523

    Can I replace 3137 with this: 3137,3032,2352,2523,2523 if I want the function work for several elements?

    #2298748
    David
    Staff
    Customer Support

    Hi there,

    you can use a PHP Snippet like so:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 99 === $element_id && is_paged() ) {
            $display = false;
        }
    
        return $display;
    }, 10, 2 );

    You need to change the 99 to the ID of your Element ( which can be found in the Browser URL when editing the element ).

    How to add PHP:
    https://docs.generatepress.com/article/adding-php/

    #2295334
    Ying
    Staff
    Customer Support

    Hi David,

    Try this php snippet, replace 2560with your content template element ID.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
    
        global $post;
    
        if ( 2560 === $element_id && is_post_type_archive() && !$post ) {
    
            $display = false;
    
        }
    
        return $display;
    
    }, 10, 2 );
Viewing 15 results - 1 through 15 (of 162 total)