WordPress.org

Ready to get started?Download WordPress

Forums

Including Pages and their Children with wp_list_pages() (5 posts)

  1. dougwaltman
    Member
    Posted 5 years ago #

    I would like to use the wp_list_pages() function to display the menu menu on one of our websites. The menu structure has several pages, some of which have children. If I use the include parameter, I only get the top level pages. Is it even possible to have the children of those pages appear without having to list every single one?

    Example structure:

    parent (1)
    child (4)
    child (5)
    parent (2)
    child (6)
    parent (3)
    child (7)
    child (8)

    Using wp_list_pages("include=1,2,3") gives me:

    parent(1)
    parent(2)
    parent(3)

    I want all of the ancestors to be displayed as well, instead of having to include every child in the list like this: wp_list_pages("include=1,2,3,4,5,6,7,8"). Which is tedious and requires maintenance every time a page is added or removed.

  2. esmi
    Forum Moderator
    Posted 5 years ago #

  3. dougwaltman
    Member
    Posted 5 years ago #

    Here is a workaround. I started with an array of pages IDs I want to include. I loop through that array, displaying each parent page. I then call wp_list_pages again, looking for the child pages, and add them if they exist.

    $page_ids = array(1,2,3);
    foreach($page_ids as $page_id) {
      $page = wp_list_pages("echo=0&title_li=&include=$page_id");
      $submenu = wp_list_pages("echo=0&title_li=&child_of=$page_id&depth=1");
      if($submenu) $page = str_replace("</li>", "<ul>$submenu</ul></li>", $page);
      echo $page;
    }

    This code is very clunky, and calls the wp_list_pages multiple times, doing what I assume will be multiple database calls. Not very efficient or pretty, IMO. At least it does the trick for now.

    The ideal solution would be to have an "include_tree" parameter for the wp_list_pages function.

  4. wpelvis
    Member
    Posted 4 years ago #

    have exactly the same problem.
    include_tree would be ideal.

  5. mhuntdesign
    Member
    Posted 4 years ago #

    There is an exclude pages navigation plugin that may help.

    http://wordpress.org/extend/plugins/exclude-pages/

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.