[Resolved] Apply element to all children pages of children pages with snippet

Home Forums Support [Resolved] Apply element to all children pages of children pages with snippet

Home Forums Support Apply element to all children pages of children pages with snippet

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2350545
    Max

    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!

    #2350946
    Ying
    Staff
    Customer Support

    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 );
    #2370878
    Max

    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 );
    #2371038
    David
    Staff
    Customer Support

    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.

    #2371276
    Max

    Ok, thanks!

    #2371288
    David
    Staff
    Customer Support

    You’re welcome

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.