WordPress.org

Ready to get started?Download WordPress

Forums

Display list of child pages plus all parent pages (3 posts)

  1. Filespit
    Member
    Posted 1 year ago #

    Hello!

    I am having trouble displaying a list of parent pages and child pages. On my front page I want to display all my pages (a list of products), which I simply do with wp_list_pages, but with a few exclusions which I do with exclude.

    Each product is a parent page, but only a few of them have children. When I click on a parent page that has children, I want the children for that page to be displayed, but I still want to display all the other parent pages. Other parent pages with children shall not have their children displayed (probably done with is_page?).

    Also, if I click on a child page, it displays the same list as if I'm visiting a parent page. The page that I'm visiting should also have an "active" css class.

    I suppose this is done using depth and child_of in wp_list_pages, but I just can't figure it out? Can anyone help?

  2. esmi
    Forum Moderator
    Posted 1 year ago #

  3. Filespit
    Member
    Posted 1 year ago #

    No luck there I'm afraid. The only thing I manage to do is display the child pages of a parent page. The sibling parent pages all disappear, which I don't want them to. This does seem to do the work, but it displays nothing for me, and completely removes the_content() as well.

    <?php
    // use wp_list_pages to display parent and all child pages all generations (a tree with parent)
    $parent = 93;
    $args=array(
      'child_of' => $parent
    );
    $pages = get_pages($args);
    if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
    
      $args=array(
        'title_li' => 'Tree of Parent Page ' . $parent,
        'include' =>  $parent . ',' . implode(",", $pageids)
      );
      wp_list_pages($args);
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic