Sub Pages ONLY For Current Parent Page
-
Alright I’ve got a theme going using mostly pages. I’ve got a set of parent pages, and each one has multiple children pages. If I want to call a list of all the top level pages in the sidebar, I use:
<ul> <?php wp_list_pages('title_li=&sort_column=menu_order&depth=1')); ?> </ul>
Which works fine. When someone clicks on a top level page, I want the sidebar to now display a list of top level pages, with the children ‘expanded’ under the top level page that is currently being viewed. Right now I’m using this:
<ul> <?php wp_list_pages('title_li=&sort_column=menu_order&depth=2')); ?> </ul>
Which works, but lists ALL children pages under ALL parent pages. I hid the other, unwanted children using CSS, but I’d rather just not even have them on the page. Without using a plugin, is there a way to modify that call to show all parent pages, along with the children pages of ONLY the current parent?
-
Try this one..
<?php wp_list_pages('title_li=&sort_column=menu_order&depth=1'); ?> <?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&depth=1"); if ($children) { ?> <ul> <?php echo $children; ?> </ul> <?php } ?>
I’m not 100% sure, but I think that what clarklab was talking about is more like this, I’m also looking for a similar, non-css based solution working with the wp_list_pages() function and not using a plugin.
Let’s assume that we have 5 top-level pages and that ONLY top-level pages 2 and 4 have sub-pages (children). Our navigation hierarchy, fully expanded looks like this:
Top 1
Top 2
— Sub 1
— Sub 2
— Sub 3
Top 3
Top 4
— Sub 1
— Sub 2
— Sub 3
Top 5Now here’s how that should display in the sidebar…
If we’re on a top-level page that has no children:
Top 1
Top 2
Top 3
Top 4
Top 5If we’re on a top-level page that has children:
Top 1
Top 2 (Current)
— Sub 1
— Sub 2
— Sub 3
Top 3
Top 4
Top 5Essentially, we’re showing all top-level elements at all times, but only showing the children of the current (active) top-level element.
Any ideas?
This works for me:
$output= ''; if($curr_post->post_parent) $children = wp_list_pages("title_li=&child_of=".$curr_post->post_parent."&echo=0"); else $children = wp_list_pages("title_li=&child_of=".$curr_post->ID."&echo=0"); if ($children) { $output .= '<h3>Pages in This Section</h3><ul class="showchildren">'."\n"; $output .= $children; $output .= "</ul>\n"; } echo $output;
Hrmmm for me that just prints out all top level pages and children. I disabled all plugins and put that snippet in an empty sidebar.php and that’s the result for me.
I did find a solution though…
I installed 2 extensions, even though I was trying to avoid it. A combination of Fold Page List and List Pages Plus allowed me to achieve exactly the navigation I wanted and then style it with SIFR.
ltmayonnaise-
could you provide some insight as to how you were able to combine those two plugins to achieve the results you described?
i am looking to do exactly that.
<?php $Pages = wp_list_pages('title_li=&echo=0&depth=1'); $InnerPages = wp_list_pages('child_of='.($post->post_parent != false ? $post->post_parent : $post->ID).'&title_li=&echo=0'); $Title = ($post->post_parent != false) ? trim(get_the_title($post->post_parent)) : trim(wp_title('', false)); if($Title != '') $Pages = str_replace($Title.'</a></li>', $Title.'</a>'. '<ul>'.$InnerPages.'</ul></li>', $Pages); echo $Pages; unset($Pages, $InnerPages); ?>
It’s a slightly dirty method but it works…
@sayhitovincent: you da man! This is just what I needed, thanks!
Hmm, actually that doesn’t do the trick.
Has someone solved this? I mean, a collapsable menu with wp_list_pages?
I think it would have to be something like this:
loop pages
if has_children & is_active
loop children
- The topic ‘Sub Pages ONLY For Current Parent Page’ is closed to new replies.