WordPress – hide childrens of subpages in wp_list pages

I have following page structure:

  • Main page (id 5)
    • Subpage 1
      • children page
      • children page
    • Subpage 2
      • children page
      • children page

I use this code for displaying my sidebar menu:

Read More
 <?php wp_list_pages('child_of=5&title_li=&link_before=<span>&link_after=</span>'); ?>

This code displays only a list. I would need to display

  • when on the main page, display only subpages
  • when on the subpage, display subpages and children pages of the subpage I’m currently on
  • when on the children page, display the display subpages and children pages of the parent subpage I’m currently on

Any idea how to do this?
Thanks for help

Related posts

Leave a Reply

3 comments

  1. Use Depth to handle what you want it to show

    <?php wp_list_pages('child_of=5&depth=1&title_li=&link_before=<span>&link_after=</span>'); ?>
    

    Reference: http://codex.wordpress.org/Function_Reference/wp_list_pages

    The default value is 0 (display all pages, including all sub-pages).

        0 (default) Displays pages at any depth and arranges them hierarchically in nested lists
        -1 Displays pages at any depth and arranges them in a single, flat list
        1 Displays top-level Pages only
        2, 3 … Displays Pages to the given depth
    
  2. You could try to use this Hierarchical Pages Widget. I’m not sure if it supports exactly what you want, but I used it on one of my projects and it worked great.

    This widget will:

    Make collapsing hierarchical pages/category/taxonomy lists: top
    level; ancestors, children, and/or siblings of current

  3. I found a working code, works like a charm for me:

    /**
     * Create HTML list of pages.
     *
     * @package Razorback
     * @subpackage Walker
     * @author Michael Fields <michael@mfields.org>
     * @copyright Copyright (c) 2010, Michael Fields
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     *
     * @uses Walker_Page
     *
     * @since 2010-05-28
     * @alter 2010-10-09
     */
    class Razorback_Walker_Page_Selective_Children extends Walker_Page {
        /**
         * Walk the Page Tree.
         *
         * @global stdClass WordPress post object.
         * @uses Walker_Page::$db_fields
         * @uses Walker_Page::display_element()
         *
         * @since 2010-05-28
         * @alter 2010-10-09
         */
        function walk( $elements, $max_depth ) {
            global $post;
            $args = array_slice( func_get_args(), 2 );
            $output = '';
    
            /* invalid parameter */
            if ( $max_depth < -1 ) {
                return $output;
            }
    
            /* Nothing to walk */
            if ( empty( $elements ) ) {
                return $output;
            }
    
            /* Set up variables. */
            $top_level_elements = array();
            $children_elements  = array();
            $parent_field = $this->db_fields['parent'];
            $child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;
    
            /* Loop elements */
            foreach ( (array) $elements as $e ) {
                $parent_id = $e->$parent_field;
                if ( isset( $parent_id ) ) {
                    /* Top level pages. */
                    if( $child_of === $parent_id ) {
                        $top_level_elements[] = $e;
                    }
                    /* Only display children of the current hierarchy. */
                    else if (
                        ( isset( $post->ID ) && $parent_id == $post->ID ) ||
                        ( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
                        ( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
                    ) {
                        $children_elements[ $e->$parent_field ][] = $e;
                    }
                }
            }
    
            /* Define output. */
            foreach ( $top_level_elements as $e ) {
                $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
            }
            return $output;
        }
    }