- This topic has 5 replies, 3 voices, and was last updated 2 years, 2 months ago by David.
-
AuthorPosts
-
September 22, 2022 at 6:33 am #2350545Max
Hi there!
I have an element that I want to apply to all children of a page.
I managed that by creating a snippet:
`add_filter( ‘generate_element_display’, function( $display, $element_id ) {
global $post;// Adds header-without-menu to all children of [Checkout]
if ( 12345 === $element_id && ( is_page() && $post->post_parent == ‘67890’ ) ) {
$display = true;This works great, but I noticed that children of those children pages don’t have the element.
Is there a way to modify this snippet so that the element also shows on all the children of children?
Thanks!
September 22, 2022 at 11:23 am #2350946YingStaffCustomer SupportHi Max,
Try this:
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; $child_pages = get_pages( array( 'child_of' => 67890, ) ); $child_pages_ids = wp_list_pluck( $child_pages, 'ID' ); if ( 12345 === $element_id && ( is_page($child_pages_ids)) ) { $display = true; } return $display; }, 10, 2 );
October 12, 2022 at 4:40 am #2370878MaxThanks a lot, snippet seems to work well!
Is there a clean way to do this for multiple children pages?
I’m currently using this: https://jmp.sh/u8kmnMk
How do I change the code so that it works like the snippet you provided? Do I simply repeat everything between the first & last line?
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; $child_pages = get_pages( array( 'child_of' => 77777, ) ); $child_pages_ids = wp_list_pluck( $child_pages, 'ID' ); if ( 88888 === $element_id && ( is_page($child_pages_ids)) ) { $display = true; } return $display; global $post; $child_pages = get_pages( array( 'child_of' => 55555, ) ); $child_pages_ids = wp_list_pluck( $child_pages, 'ID' ); if ( 66666 === $element_id && ( is_page($child_pages_ids)) ) { $display = true; } return $display; }, 10, 2 );
Or is it better to just use the add_filters on top of each other?
add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; $child_pages = get_pages( array( 'child_of' => 77777, ) ); $child_pages_ids = wp_list_pluck( $child_pages, 'ID' ); if ( 88888 === $element_id && ( is_page($child_pages_ids)) ) { $display = true; } return $display; }, 10, 2 ); add_filter( 'generate_element_display', function( $display, $element_id ) { global $post; $child_pages = get_pages( array( 'child_of' => 55555, ) ); $child_pages_ids = wp_list_pluck( $child_pages, 'ID' ); if ( 66666 === $element_id && ( is_page($child_pages_ids)) ) { $display = true; } return $display; }, 10, 2 );
October 12, 2022 at 7:26 am #2371038DavidStaffCustomer SupportHi there,
if for each Child Page, you’re wanting to display a different element then adding filters on top of each other makes the most sense.
October 12, 2022 at 8:36 am #2371276MaxOk, thanks!
October 12, 2022 at 8:48 am #2371288DavidStaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.