WordPress.org

Ready to get started?Download WordPress

Forums

Sidebar - Show only pages of parent (1 post)

  1. FeRo2991
    Member
    Posted 10 months ago #

    Hi!
    I'm trying to create a sidebar using this code:

    <?php
    
    		global $post;     // if outside the loop
    		$children = get_pages('child_of='.$post->ID);
    
    		if ( (is_page() && $post->post_parent) || count( $children ) != 0 ) {
    		echo "<h2>Navigation</h2>";
    
    			echo '<ul class="parent">';
    			// Use parent page if exists, else use current page
    			$child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
    			if( $post->post_parent )
    			{
    				$parent = get_post($post->post_parent);
    				$child_of_value = ( $parent->post_parent ? $parent->post_parent : $post->post_parent );
    			}
    
    			// Depth of 2 if parent page, else depth of 1
    			$depth_value = ( $post->post_parent ? 2 : 1 );
    
    			// Build argument array
    			$wp_list_pages_args = array(
    			    'child_of' => $child_of_value,
    			    'depth' => $depth_value,
    				'exclude' => '',
    			    'title_li' => ''
    			);
    			// Now, output the result
    			wp_list_pages( $wp_list_pages_args );
    		echo '</ul>';
    
    		} else {
    		    // This is not a subpage or a parent
    			$kiddies = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    			echo "<script>$('article').css( 'width', '720px' );</script>";
    		}
    		?>

    So there are 3 depths, the granparent is working fine, but when you click on the parent or child ALL the children are displayed and I want that only the children of the branch I'm in are displayed e.g.:

    A
    -a
    -b
    -c
    B
    -d
    -e
    -f

    When you click on the "A" or its children "abc" only "abc" should be displayed, NOT "def".
    I thought about somehow excluding the other sites in $wp_list_pages_args, but I don't know how to get to them..

    Help is very much appreciated! Thank you in advance!

Reply

You must log in to post.

About this Topic