- This topic has 31 replies, 4 voices, and was last updated 3 years, 6 months ago by
David.
-
AuthorPosts
-
May 24, 2022 at 7:41 am #2230935
David
StaffCustomer SupportCan you retry the method Ying provided here:
https://generatepress.com/forums/topic/display-rules-for-elements-regex-possible/#post-2215710
May 24, 2022 at 12:14 pm #2231391Maxime
Perfect, it works now. The problem was related to the outdated plugin.
Thank you for your help 🙂
May 25, 2022 at 1:22 am #2231862David
StaffCustomer SupportAwesome – glad we could be of help
May 27, 2022 at 1:02 am #2234764Maxime
Hello (again),
My Issue is not totaly solved because in some cases, a child page can have children of their own.
Example :
https://mywebsite.com/directory1/page-1/page-11
https://mywebsite.com/directory1/page-1/page-12
https://mywebsite.com/directory1/page-1/page-13>> I want to apply my rules to all my children and grandchildren pages.
I found this solution (https://generatepress.com/forums/topic/block-hook-on-all-sub-pages/)with the fonction
get_post_ancestorsin my case the full code is :
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; $ancestors = get_post_ancestors( 3854 ); if ( 3850 === $element_id && 3854 == $post->post_parent || is_single( $post->post_parent ) || in_array( $post->ID, $ancestors ) ){ $display = true; } return $display; }, 10, 2 );As a reminder:
3850 is my Element ID
3854 is my parent page ID (/directory1/)With this function it still work for the children pages but not for my grandchildren pages.
>> where is the issue?
Thank you a lot for your help.
May 27, 2022 at 8:08 am #2235406David
StaffCustomer SupportGive this a shot:
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; // Get the top level parent ID if ($post->post_parent) { $ancestors=get_post_ancestors($post->ID); $root=count($ancestors)-1; $parent = $ancestors[$root]; } else { $parent = $post->ID; } if ( is_page() && 5281 === $element_id && '3854' == $parent ) { $display = true; } return $display; }, 10, 2 );May 27, 2022 at 8:34 am #2235431Maxime
Unfortunately it doesn’t work . (I adapted the ID and removed an extra paranthesis):
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; // Get the top level parent ID if ($post->post_parent) { $ancestors=get_post_ancestors($post->ID); $root=count($ancestors)-1; $parent = $ancestors[$root]; } else { $parent = $post->ID; } if ( is_page() && 3850 === $element_id && '3854' == $parent ) { $display = true; } return $display; }, 10, 2 );May 27, 2022 at 8:46 am #2235446David
StaffCustomer SupportOops sorry about the extra
).I tested this on my local install, with an Element set to Display on the parent and it worked.
We are dealing with static pages right ?May 27, 2022 at 9:06 am #2235467Maxime
Yes we are dealing with static pages.
May 27, 2022 at 9:33 am #2235494David
StaffCustomer Supportbefore this line:
if ( is_page() && 3850 === $element_id && '3854' == $parent ) {can you add:
var_dump($parent);Does this return any data when you view the parent or child pages on the front end ?
May 27, 2022 at 9:47 am #2235503Maxime
It doesn’t work but Curently the following characters are displayed at the top of the pages in front :
int(9) int(9) int(9) int(9) int(9) int(9) int(9) int(9) int(9)These characters are the same for all pages in the directory (children pages and grandchildren pages). But logically different (I think) for other pages.
May 27, 2022 at 11:52 am #2235596David
StaffCustomer SupportThat INT – should be the Page ID of the Parent page – can you try that in the condition ?
May 27, 2022 at 1:09 pm #2235635Maxime
Yes it is the ID of my level 1 parent. I forgot to specify one level in my url. The complete URL is : https://mywebsite.com/section-1/directory1/page-1/page-11
“Section1” ID : 9
“Directory1” ID : 3854And now, I have the result I wanted, Thank you 🙂
May 28, 2022 at 10:29 am #2236319David
StaffCustomer SupportGlad to be of help!
October 28, 2022 at 2:27 pm #2392199Maxime
Hello,
I am re-launching an old topic because I would like another clarification:
This rule is working well :
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; if ( 490 === $element_id && ( is_page() && $post->post_parent == '9' ) ) { $display = true; } return $display; }, 10, 2 );But is it possible to exclude a specific page from this rule ? (in order to assign another element in the back office). I tried to modify my condition but nothing change (I want to exclude the page which the ID is 35):
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; if ( 490 === $element_id && ( is_page() && $post->post_parent == '9' ) && ( is_page() && $post->post !== '35' )) { $display = true; } return $display; }, 10, 2 );What is the issue?
Thank you for your help.
October 29, 2022 at 6:57 am #2392689David
StaffCustomer SupportHi there,
try:
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; if ( 490 === $element_id && ( is_page() && $post->post_parent == '9' ) && !is_page( 35 ) ) { $display = true; } return $display; }, 10, 2 ); -
AuthorPosts
- You must be logged in to reply to this topic.