[Resolved] Conditionally Display Hook on Sub-page of Parent

Home Forums Support [Resolved] Conditionally Display Hook on Sub-page of Parent

Home Forums Support Conditionally Display Hook on Sub-page of Parent

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #760549
    Donnie

    I am working to conditionally display GP Elements on sub-pages of a specific parent.

    In the example below, I am attempting to use the “generate_hook_element_display” filter in concert with the post_parent function to build the logic necessary to accomplish this.

    The code I’m working with is as follows:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 951 === $element_id && ( is_page() && $post->post_parent > 77 ) ) {
            $display = true;
        }
        return $display;
    }, 10, 2 );

    In the example above:
    951 = GP Element / Hook
    77 = Parent Page ID
    73 = Child Page ID (though not used in the above snippet).

    Thanks for your help.

    #760715
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You’re using the $post variable, but it’s not defined.

    Try adding this above your conditional:

    global $post;

    You’re also using >, which is greater than. You might want to try == instead.

    Let me know ๐Ÿ™‚

    #760775
    Donnie

    Thanks Tom,

    That’s absolutely the nudge in the right direction I needed. Thank you for your help.

    For those who may come across this thread in the future, here’s the final code that worked for me:

    global $post;
    
    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 951 === $element_id && ( is_page() && $post == post_parent > 77 ) ) {
            $display = true;
        }
        return $display;
    }, 10, 2 );
    #761392
    Tom
    Lead Developer
    Lead Developer

    Glad I could help – thanks for sharing your code!

    You may want to place global $post inside the filter though, directly above your conditional ๐Ÿ™‚

    #843660
    Joe

    Would love to know how you executed this. I’m trying to have a Layout Element execute on all subpages of a specific page parent. I tried adding your code as a Snippet (Snippets plugin) but it had no effect. What Display Condition did you set for your Element? That seems to be the only thing GP responds to.

    #843922
    Tom
    Lead Developer
    Lead Developer

    Using the filter bypasses the Display Rules.

    For a Layout Element, you’d do this:

    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 951 === $element_id && ( is_page() && $post == post_parent > 77 ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    #844636
    Joe

    Thanks Tom. I was using the wrong filter. Your code didn’t quite work but this code did:

    
    add_filter( 'generate_layout_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 1180 === $element_id && ( is_page() && $post->post_parent == '415' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    

    For anyone else using this, you’ll want to be sure NOT to set any Display Conditions because this doesn’t actually override the Display Conditions but rather appends. Of course if you do want to set Display Conditions that are available with Elements then go right ahead. Both will take effect.

    Here’s an example:

    You could use the Elements Display Conditions to set your Layout on a parent page (ID ‘415’ in this example) and then use this Code Snippet to set that same Layout for all the child pages of that page parent.

    Hope this helps.

    #844727
    Tom
    Lead Developer
    Lead Developer

    Thanks for sharing the solution! Appreciate it ๐Ÿ™‚

    #1077504
    Rafaล‚

    Hi!
    Does the filter stop working in latest GP release??
    How to easily add Display Rules for all descendant pages of one parent?

    #1078201
    Leo
    Staff
    Customer Support

    The code should still work.

    Did you make sure to element ID and parent page ID?

    How are you adding the code?

    #1079156
    Rafaล‚

    I’ve made.
    I’ll try the code on a clean instance, maybe, someday ๐Ÿ˜‰

    #1080104
    Leo
    Staff
    Customer Support

    Let me know if you need another look ๐Ÿ™‚

    #1092863
    Rafaล‚

    OK, I was blind, and I’ve been struggling to make effect with display rules on my hook, but using layout tag ๐Ÿ˜›
    My specific need was to inject some code on 2 selected pages (eg. id: 111 and 222) and all their children (sub-pages).
    This works for me as Display Rules on Hook (id: 888):

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
      global $post;
    
      if ( $element_id===888 && 
           ( is_page(array(111,222)) || 
             is_page() && $post->post_parent===111 || 
             is_page() && $post->post_parent===222 
           ) 
         ) {
         $display = true;
      }	
      return $display;
    }, 10, 2 );

    The filter added to functions.php of my child theme, of course ๐Ÿ™‚

    #1093493
    Tom
    Lead Developer
    Lead Developer

    Glad you got it sorted ๐Ÿ™‚

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