Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Display Element Snippet Array

  • Hey there, can you please check the following snippet. I changed it a bit for the use of an array. I dont want, that these elements are used for the parent page, only for the child pages.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    	
      if ( $element_id===1508 && 
           ( is_page() && $post->post_parent===2013 || 
             is_page() && $post->post_parent===2139 ||
    		 is_page() && $post->post_parent===2144 ||
    		 is_page() && $post->post_parent===2142 ||
    		 is_page() && $post->post_parent===2148 ||
    		 is_page() && $post->post_parent===2146
           ) 
         ){
         $display = false;
      }
    	  if ( $element_id===2025 && 
           ( is_page() && $post->post_parent===2013 || 
             is_page() && $post->post_parent===2139 ||
    		 is_page() && $post->post_parent===2144 ||
    		 is_page() && $post->post_parent===2142 ||
    		 is_page() && $post->post_parent===2148 ||
    		 is_page() && $post->post_parent===2146
           ) 
         ){
         $display = true;
    	  }
        return $display;
    }, 10, 2 );

    The reason im asking is, that some child pages didnt show the featured image in the hero element i want to display until i change the featured image. Thought maybe the snippet isnt entirely correct.

  • Hi there,

    so there are 2 GP elements: 1508 and 2025
    If in GP Elements: 1508 is set to Display, and 2025 is NOT set to display.
    Then that function should “swap” them on any child page that has a parent ID that matches: 2013, 2139, 2144, 2142, 2148, 2146

    If thats correct, then that function should work as expected.
    Was it possible the page was cached ? So it showed out of date content.

    As an aside you could probably simplify that function to something like this:

    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
        
        $allowed_parents = array(2013, 2139, 2144, 2142, 2148, 2146);
        
        if ( in_array($post->post_parent, $allowed_parents) && is_page() ) {
            if ( $element_id === 1508 ) {
                $display = false;
            } elseif ( $element_id === 2025 ) {
                $display = true;
            }
        }
        
        return $display;
    }, 10, 2 );
    
  • ok, sounds good. Will try the improved version and will keep an eye on it. Caching isnt activated but if it works now, i dont care 😀

    Thank you David!

  • You’re welcome !

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