wp_list_pages: only show subpages on the parent page?

I’m using wp_list_pages('title_li=') on my site.

Some of my pages do have subpages, however I don’t want to list them unitl I’m on an actual parent page that has subpages.

Read More

So imagine my front-page:

— About Us
— Gallery
— Kitchen
— Disclaimer

When clicking on Gallery (and Gallery has two subpages) I want them to be listed as well.

— About Us
— Gallery
  — Subpage 1
  — Subpage 2
— Kitchen
— Disclaimer

How am I going to do this with the wp_list_pages() function?

Related posts

Leave a Reply

2 comments

  1. This would probably be better achieved using CSS. First, you hide all .children:

    .page_item .children {
      display: none;
    }
    

    Then, you show the current_page_item’s children:

    .current_page_item .children {
      display: block;
    }
    
  2. try the following:

    $output = wp_list_pages(depth=1);
    

    expl:
    depth
    (integer) This parameter controls how many levels in the hierarchy of pages are to be included in the list generated by 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 
    

    For mor info can have a look here