[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.

    GeneratePress 2.2.1
    GP Premium 1.7.7
    #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.