- This topic has 13 replies, 5 voices, and was last updated 3 years, 6 months ago by
Tom.
-
AuthorPosts
-
December 18, 2018 at 12:35 pm #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.
December 18, 2018 at 5:33 pm #760715Tom
Lead DeveloperLead DeveloperHi 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 ๐
December 18, 2018 at 7:40 pm #760775Donnie
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 );
December 19, 2018 at 9:56 am #761392Tom
Lead DeveloperLead DeveloperGlad I could help – thanks for sharing your code!
You may want to place
global $post
inside the filter though, directly above your conditional ๐March 19, 2019 at 11:17 am #843660Joe
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.
March 19, 2019 at 4:51 pm #843922Tom
Lead DeveloperLead DeveloperUsing 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 );
March 20, 2019 at 8:05 am #844636Joe
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.
March 20, 2019 at 9:19 am #844727Tom
Lead DeveloperLead DeveloperThanks for sharing the solution! Appreciate it ๐
November 26, 2019 at 1:11 am #1077504Rafaล
Hi!
Does the filter stop working in latest GP release??
How to easily add Display Rules for all descendant pages of one parent?November 26, 2019 at 8:40 am #1078201Leo
StaffCustomer SupportThe code should still work.
Did you make sure to element ID and parent page ID?
How are you adding the code?
November 27, 2019 at 12:45 am #1079156Rafaล
I’ve made.
I’ll try the code on a clean instance, maybe, someday ๐November 27, 2019 at 9:31 am #1080104Leo
StaffCustomer SupportLet me know if you need another look ๐
December 4, 2019 at 3:00 am #1092863Rafaล
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 ๐December 4, 2019 at 10:31 am #1093493Tom
Lead DeveloperLead DeveloperGlad you got it sorted ๐
-
AuthorPosts
- You must be logged in to reply to this topic.