Support » Fixing WordPress » Including Pages and their Children with wp_list_pages()

  • 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:


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi


    Forum Moderator

    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.

    have exactly the same problem.
    include_tree would be ideal.

    There is an exclude pages navigation plugin that may help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Including Pages and their Children with wp_list_pages()’ is closed to new replies.