Archived topic
Apply element to all children pages of children pages with snippet
5 replies · Started by Max on September 22, 2022
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!
Hi 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 );
Thanks 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 );
Hi 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.
Ok, thanks!
You're welcome