Question

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!

No correct solution

OTHER TIPS

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

    <?php if(is_page()){
$ancestors = get_post_ancestors($post);

if (count($ancestors) == 0) {
    $children = wp_list_pages("sort_column=post_date&title_li=&child_of=".$post->ID."&echo=0&depth=1");
     //echo 'test0';
}elseif (count($ancestors) == 1) {
    $parent = $ancestors[0];
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=" .$parent. "&echo=0");
    //echo 'test1';
 }elseif (count($ancestors) == 2) {
    $parent = $ancestors[1];
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=" .$parent. "&echo=0");
    //echo 'test2';
 }elseif (count($ancestors) == 3) {
    $parent = $ancestors[2];
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=" . $parent. "&echo=0");
    //echo 'test3';
}elseif (count($ancestors) == 4) {
    $parent = $ancestors[3];
    $children = wp_list_pages("sort_column=menu_order&title_li=&child_of=" . $parent . "&echo=0");
    //echo 'test4';
}
  ?>

    <?php if($children){ ?>
    <ul class="leftmenu">
    <?php echo $children; ?>
    </ul>
    <?php }  }?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top