WordPress.org

Ready to get started?Download WordPress

Forums

dynamically limit depth of wp_list_pages (4 posts)

  1. vadims00
    Member
    Posted 1 year ago #

    I guess that this should be an easy one but I haven't yet find the proper solution.
    I want to limit the depth of wp_list_pages so as not to display the pages of the last level. So supposing that parent page A has 3 levels of pages I only want to list first 2 levels. I was looking for a function to count the maximum depth of children for a page but no success.
    Till now my code is

    $parents = get_post_ancestors($post->ID);
    	if (count($parents)>1) {
    		$parent = $parents[count($parents)-2];
    	} else	{
    		$parent = $post->ID;
    	}
    
    	$args = array(
                'depth'        => 0,  // this should be set dynamically
                'child_of'     => $parent,
                'sort_column'  => 'menu_order, post_title',
                'post_type'    => 'page',
                'post_status'  => 'publish'
            ); 
    
    		$page_nav = wp_list_pages( $args );

    Any help is greatly appreciated

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Actually, I don't think it is that easy. Please explain what you would show for posts like this:

    parent
    ---child a
    ---child b
    ------child ba
    ------child bb
    ---------child bba
    ---------child bbb
    ---------child bbc
    ---------child bbd
    ------child bc
    ---------child bca
    ------------child bcaa
    ---------------child bcaaa

  3. vadims00
    Member
    Posted 1 year ago #

    It whould be like this

    parent
    ---child a

    ---child b
    ------child ba
    ------child bb

    ------child bc
    ---------child bca
    ------------child bcaa

    which means all levels of each child except the last level.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Well, it definitely is not simple, but I think I at least got close. The code is here: http://pastebin.com/pV5pe2S0

Topic Closed

This topic has been closed to new replies.

About this Topic