0

I'm looking (without having to copy the whole code) that when the child page has child pages of it's own that it wil not show those child pages but still the parrent page 2 levels up. i played with it but in my opinion i have to copy the whole code, change the level setting, and give a different shortcode name.

    function wpb_list_child_pages_popup() 
{
    // Define our $string variable
    $string = '';

    // Make sure this is a page
    if ( !is_page() )
        return $string;

    $post = $GLOBALS['wp_the_query']->get_queried_object();
    // Make sure the current page is not top level
    if ( 0 === (int) $post->post_parent ) {
        $parent = $post->ID;
    } else {
        $ancestors = get_ancestors( $post->ID, $post->post_type );
        // Check if $ancestors have at least two key/value pairs
        if ( 1 == count( $ancestors ) ) {
            $parent = $post->post_parent;
        } else {
            $parent = $ancestors[1]; // Gets the parent two levels higher
        }
    }  

    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $parent . '&echo=0' );

    $string .= '<ul id="child-menu">' . $childpages . '</ul>';

    return $string;

}

add_shortcode('wpb_childpages_popup', 'wpb_list_child_pages_popup');

Code - Thanks to Pieter Goosen

1 Answer 1

1

You can do that with using the depth argument. I also created an array of your arguments for better readability.

The depth argument accepts the following parameters:

'depth' (int) Number of levels in the hierarchy of pages to include in the generated list. Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to the given n depth). Default 0.

So, as you want to display the first children, you need to set it to 2, if you want the next level included, set it to three. 1 gives you toplevel ($parent == 0) only.

$args = array(

    'sort_column' => 'menu_order',
    'title_li'    => '',
    'child_of'    => $parent,
    'echo'        => 0,
    'depth'       => 2 

);
$childpages = wp_list_pages( $args );
1
  • That's not quitte what i'm looking for. The code that i have works. But now i wanna know how to (without copying the whole code a 2nd time) make this work. child page has child pages of it's own and that it wil not show those child pages but still the parrent page 2 levels up.
    – Dionoh
    Commented Mar 2, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.