My Goal:
This is kind of tricky, so bear with me. I’ve got a 3-tier page organization on a WordPress website:
Tier 1
- Tier 2
- Tier 2
- Tier 3
- Tier 3
- Tier 2
- Tier 2
- Tier 3
- Tier 2
If I use wp_list_pages
, I get can get a list of pages. I need to show only certain pages though. I’ve broken it down into four possible sets. Either the user is on a Tier 1
page, a Tier 2
page without children, a Tier 2
page with children, or a Tier 3
page.
Tier 1 Desired Results:
- Tier 2
- Tier 2
- Tier 2
- Tier 2
- Tier 2
Tier 2 Desired Results (If Page doesn’t have children):
- Tier 2 (<- Active Page)
- Tier 2
- Tier 2
- Tier 2
- Tier 2
Tier 2 Desired Results (If Page does have children):
- Tier 2
- Tier 2 (<- Active Page)
- Tier 3
- Tier 3
- Tier 2
- Tier 2
- Tier 2
Tier 3 Desired Results
- Tier 2
- Tier 2
- Tier 3 (<- Active Page)
- Tier 3
- Tier 2
- Tier 2
- Tier 2
My Attempt:
<?php
if ($post->post_parent) { //SUB PAGE
$depth = 1;
if ( count(get_pages('child_of=' . $post->ID)) ) { $depth=0; }
$children = wp_list_pages("sort_column=post_date&title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
}
else { //TOP PAGE
echo "Level 1";
$children = wp_list_pages("sort_column=post_date&title_li=&child_of=".$post->ID."&echo=0&depth=1");
} ?>
<ul id="subNav">
<?php echo $children; ?>
</ul>
Current Result
The above code almost works. Tier 1
works, Tier 2
without a child works. But Tier 3
with a child displays ALL children, not just the children of the current page. I have been told this can be done with CSS, and if I really have to I’ll do that, but I’d really love a full PHP solution. Thank you for the help!
With this i have 4th level working, hope it will help you, to tweak it edit a $parent = $ancestors[0]; etc , also you can uncomment //echo to see whats going on and where 🙂 regards