Grow your CSS skills. Land your dream job.

WordPress: Displaying sub-pages only for the current page

  • # August 8, 2009 at 10:47 pm

    I’m working on the navigation in a wordpress theme (done in the sidebar) and I can’t figure out how to make the list of pages only show the subpages for the current page and other page’s subpages not to be shown.

    thus far I’ve been trying to figure out a way to do it with: wp_list_pages(‘ title_li’ ); but to no avail

    here’s an Example:

    Pages:
    -home <— on this one
    -subpage1
    -subpage2
    -Blog
    -FAQ

    Pages:
    -home
    -Blog
    -FAQ<— on this one
    -subpage1
    -subpage2

    Pages:
    -home
    -Blog<—on this one (no subpages)
    -FAQ

    # August 9, 2009 at 2:47 am

    It’s all there in the codex http://codex.wordpress.org/Template_Tags/wp_list_pages#List_subpages_even_if_on_a_subpage

    To list just the children of the current page (or its siblings if already on a child page):

    Code:
    < ?php
    if($post->post_parent)
    $children = wp_list_pages(“title_li=&child_of=”.$post->post_parent.”&echo=0″);
    else
    $children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&echo=0″);
    if ($children) { ?>

      < ?php echo $children; ?>

    < ?php } ?>

    # August 10, 2009 at 6:09 pm

    Actually this can be achieved using just a little css – and it will work exactly as in your example too.

    Code:
    Code:
    ul#pagenavi li ul { display: none; }
    ul#pagenavi li.current_page_item ul, ul#pagenavi li.current_page_ancestor ul { display: block; }
    # August 12, 2009 at 11:59 pm

    The first one only shows the subpage

    the CSS one looks like it would work but I haven’t tested that one yet,

    Thanks you both for you time =)

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".