WordPress.org

Support

Support » Themes and Templates » Only Display Sub Pages when Top Level Page is Clicked

Only Display Sub Pages when Top Level Page is Clicked

  • Hello,
    I’m searching through the documentation and I’m not sure what I need to look for to get my page display to only show the top level pages, unless a top level page is clicked on, then I want it to show the sub pages that go under just that one top level page that was clicked. Can some one point me in the right direction?

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • There’s some examples at template tag, wp_list_pages(), and Function_Reference/get_pages

    Thanks for the info, I’m looking through the links you gave but I don’t seem to be able to find anything that will hide the sub pages until their parent page is clicked. Perhaps I need to know something more specific like the name of the argument that would accomplish this?

    Thanks!

    Use the depth=1 argument with wp_list_pages() to display all ‘top-level’ pages.

    esmi

    @esmi

    Forum Moderator

    Try this:

    <?php
    $my_pages = wp_list_pages('title_li=&depth=1&echo=0');
    if (is_page()) {
    	$this_page = $post->ID;
    	if ($post->post_parent) $this_page= $post->post_parent;
    	$children = get_page_children($this_page, get_pages());
    	if($children) {
    		$my_pages .= "<li>\n".'<ul class="subpages">';
    		foreach($children as $child) {
    			$my_pages .= wp_list_pages('title_li=&include='.$child->ID.'&echo=0');
    		}
    		$my_pages .= '</ul></li>';
    	}
    }
    echo $my_pages;
    ?>

    Thanks esmi, that works, kinda. It shows sub pages when I click on a parent page, however it shows them under the rest of the links instead of under the parent page. Is this easy to fix?

    Just to illustrate it your code makes it looks like this:

    Parent #1
    Parent #2
    Parent #3
    Parent #4
    – Child #1a
    – Child #1b

    I think that will confuse site visitors (it confuses me!). Is there a way to make it look like this?

    Parent #1
    – Child #1a
    – Child #1b
    Parent #2
    Parent #3
    Parent #4

    esmi

    @esmi

    Forum Moderator

    it shows them under the rest of the links instead of under the parent page. Is this easy to fix?

    Could be that my approach isn’t suitable for you. I sometimes show the subpage links within the parent page in a small “Related Pages” box. I have used the approach in horizontal menu with child pages appearing in on a 2nd line under the main nav but not in a vertical menu as yet.

    Sounds like you need something like the Fold Pages plugin/widget. However, be warned that it may not work under WP2.8, so please proceed with caution.

    I’ve hacked a plugin that displays a hierarchical menu. It was used on several pages and works just nice under WP2.8.

    hi! that was my thoughts with your code, esmi. it would be an elegant solution for showing child pages in a different area/box. I like the idea and would love to design a layout to accommodate such an arrangement.

    I wanted to come back and say that I found a plug in called SuperSlider-Menu. It’s doing exactly what I need and it slides which is a nice touch.

    Thanks for all the help, hopefully this thread will be helpful to someone else too.

    wlindley

    @wlindley

    you might also try the hierarchical-pages widget

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Only Display Sub Pages when Top Level Page is Clicked’ is closed to new replies.
Skip to toolbar