WordPress.org

Support

Support » How-To and Troubleshooting » wp_list_pages listing nightmare

wp_list_pages listing nightmare

  • OK. I’ve been trying for a few hours to get my navigation to show properly with wp_list_pages…

    I have many parent categories with children… for simplicity sake, each parent is A, B, C, and D. Each parent has children:, 1, 2, 3, 4.

    I am using this code:

    <?php wp_list_pages(“child_of=” . $post->ID . “&depth=01title_li=”) ?>

    This shows:
    – A
    – B
    – C
    – D

    As I want. The problem now, when I click on A, it shows me:
    – 1
    – 2
    – 3
    – 4

    When I want the child to show:
    – A
    – 1
    – 2
    – 3
    – 4
    – B
    – C
    – D

    I should also note, that parents A, B, C, D are children of a, b, c, d (of which I do not want to show the a, b, c, d at all).

    Thoughts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • You will need to use Function_Reference/get_pages to do that.

    Something like:

    <?php
    if ( is_page() ) {
      $pages = get_pages();
      foreach($pages as $page) {
        if ($page->post_parent == 0 || $page->post_parent == $posts[0]->ID ) {
        ?>
          <p><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></p>
        <?php
        }
      }
    }
    ?>

    Michael,

    Thanks for your help. This is a step in the right direction. I also found this code:

    <?php
    if (is_page()) {
      if($post->post_parent) {
        $children = wp_list_pages("child_of=".$post->ID."&echo=0&depth=1&title_li=<h2>".$post->post_parent."</h2>");
      } else {
        $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0&depth=-2");
        $children.= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      }
      if ($children) { ?>
        <ul>
        <?php echo $children; ?>
        </ul>
        <?php } } ?>

    Is there a way to combine the two?

    If I have the following:
    A
    -1
    -2
    -3
    B
    -1
    -2
    -3
    C
    -1
    –a
    –b
    –c
    -2
    -3

    I want it to show the children and parents… so, If I were on C1b the navigation would be:

    A
    B
    C
    -1
    –a
    –b
    –c
    -2
    -3

    If I were on A2:

    A
    -1
    -2
    -3
    B
    C

    If I were on B:
    A
    B
    -1
    -2
    -3
    C

    Where A, B, and C are links.

    Thoughts? I GREATLY appreciate your assistance.

    *bump*?

    Bramernic
    Member

    @bramernic

    If you display the pages to a depth that includes the lowest level you might want (eg depth=1 will show pages and sub pages) then you can use CSS to hide the children you don’t want to see. Basically, if you look at the page source you will see that every list item has a series of classes that tell you whether it is the current page, the parent of the current page or a child of the current page. Using these classes you can show or hide lines using “display none” or “display block”.

    Hope this helps.

    Hopefully someone can provide some assistance. I still have not been able to get this resolved.

    Let me try explaining again…. for each page, I need to list each parent, and the CURRENT children.

    If I am on page A2, I need it to read:

    A
    1
    2
    3
    B
    C
    D

    If I am on C1, I need it to read:
    A
    B
    C
    1
    2
    3
    D

    Does this make more sense?

    I was close with the below code, but wasn’t able to LIST the other parents.

    <?php
    if (is_page()) {
      if($post->post_parent) {
        $children = wp_list_pages("child_of=".$post->ID."&echo=0&depth=1&title_li=");
      } else {
        $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0&depth=-2");
        $children.= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      }
      if ($children) { ?>
        <ul>
        <?php echo $children; ?>
        </ul>
        <?php } } ?>

    thoughts?

    bump?

    quartermane
    Member

    @quartermane

    Hi… Does this help?

    <?php $children = wp_list_pages('child_of='.$post->ID.'&echo=1');
    if ($children) { ?>
     <?php echo $children; ?>
    <?php } ?>

    It displays a list of a parent page’s children and grandchildren; however, once you are on the child or grandchild page, it effectively becomes the parent page, and therefore does not have any children or grandchildren to list.

    It’s confusing, I know… That’s why I’m having trouble finding a solution. It’s difficult to explain what I need, but it seems like it should be easy to do.

    For my purposes, your code works well. I don’t need a list of the other parents because I have a navigation bar coded into the header. But I still need a menu that lists all of the children of a parent page on the child and grandchild pages.

    Frustrating, I know.

    If you’re curious, here’s a link to what I’m working on (it’s not very pretty): http://ecbiz85.inmotionhosting.com/~kssgco5/?page_id=100

    quartermane
    Member

    @quartermane

    Well, I’ve taken another look at what I need, and I think it’s identical to yours, lukemv:

    Parent Page
    – Child 1
    – Child 2
    – Child 3

    Child Page 1
    – Child 1
    * Grandchild 1
    * Grandchild 2
    * Grandchild 3
    – Child 2
    – Child 3

    Grandchild Page 1
    – Child 1
    * Grandchild 1
    * Grandchild 2
    * Grandchild 3
    – Child 2
    – Child 3

    I wish I was better at WP coding. For some reason, I just can’t wrap my head around it. I know my way around HTML, CSS … Even Action Script! But this is just beyond me. 😛

    OK, let’s get simpler…. how about listing a current page’s siblings, AND the children only of the current page (not the sibling’s children)…

    1
    2
    3 (current page)
    a
    b
    c
    4
    5

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘wp_list_pages listing nightmare’ is closed to new replies.