Dynamically generate a div with a list of children or siblings
-
Hey guys,
I’m trying to use WP as a CMS, and I’m trying to split pages and it’s children in two seperate navmenu’s. I have a horizontal menu which (already) displays all the parent pages (with depth=1). Now I want to have a vertical menu that displays children of the page you’re on, or children of the same family (when you’re on a child page). It will go in a div where (probably) nothing else will go. So I only want to display this div when there actually are children to show. Basically the script should do this:
If self has children: echo a div named sidebar with a list of self’s children
Elseif self is a child: echo a div with a list of self’s “siblings”
Else: echo nothing
On the wp_list_pages description I see two pieces of code that sort of do what I want. The first one generates a list only if there are indeed children, the second one will children even when on a child.
1st one:
<?php if (wp_list_pages("child_of=".$post->ID."&echo=0")) { ?> <ul> <?php wp_list_pages("title_li=&child_of=".$post->ID." &sort_column=menu_order&show_date=modified &date_format=$date_format"); ?> </ul> <?php } ?>
2nd one:
<div id="submenu"> <ul> <?php if($post->post_parent) { // page is a child wp_list_pages('sort_column=menu_order&title_li= &child_of='.$post->post_parent); } elseif(wp_list_pages("child_of=".$post->ID."&echo=0")) { // page has children wp_list_pages('sort_column=menu_order&title_li= &child_of='.$post->ID); } ?> </ul> </div>
The first one does something I want to do, namely generate html code only when there are children (I can add a <div id=”sidebar”> in it ofcourse). However, the code doesn’t seem to work. It only generates the html elements, no children are actually listed.
The second one does what I want completely, but it doesn’t dynamically generate a div or ul. They’re hardcoded I guess you could say. Could anyone think of a way to make either code (or a different one) do what I want it to do?
- The topic ‘Dynamically generate a div with a list of children or siblings’ is closed to new replies.