WordPress.org

Ready to get started?Download WordPress

Forums

List pages and subpages (4 posts)

  1. andresapitt
    Member
    Posted 3 years ago #

    Hi, I am trying without success to list in a page the children titles and their children.
    Something like:
    Programs:
    program 1
    Sub program1 1

    program 2
    Sub program2 1
    Sub program2 2
    Sub program2 3

    program 3
    Sub program3 1

    but instead I am getting:

    Programs:
    program 1
    Sub program1 1

    program 2
    Sub program2 1
    Sub program2 2
    Sub program2 3

    program 3
    Sub program3 1

    Sub program1 1
    Sub program2 1
    Sub program2 2
    Sub program2 3
    Sub program3 1

    meaning that i get the corretly listing at first but then i get the childs all totheger

    this my code:

    `<?php
    // Set up the objects needed
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

    // Get the page as an Object
    $programs = get_page_by_title('programs');

    $program_children = get_page_children($programs->ID, $all_wp_pages);

    foreach($program_children as $post) :
    echo '<h2 class="title">'.get_the_title().'</h2>';
    $program_childrenL2 = get_page_children(get_the_ID(), $all_wp_pages);
    foreach($program_childrenL2 as $postL2) :
    setup_postdata($postL2);
    echo '<h3 class="title">'.$postL2->ID.'</h3>';
    endforeach;
    endforeach;

    ?>`

  2. Helen Hou-Sandi
    WordPress Dev
    Posted 3 years ago #

    Is there a reason you aren't using the wp_list_pages function? http://codex.wordpress.org/Function_Reference/wp_list_pages
    Perhaps I'm not understanding exactly what you're using this code for.

  3. andresapitt
    Member
    Posted 3 years ago #

    Ya wp_list_pages is ok, and do a very close job to what i need but i want to have the content or excerpt as well, not just the titles of the pages.

  4. Helen Hou-Sandi
    WordPress Dev
    Posted 3 years ago #

    I believe get_page_children will get all children, which includes grandchildren, and so they show again at the bottom because they are a part of your $program_children array.

    Seems to me that you should be able to use get_pages for $all_wp_pages (though then should rename your variable) so you're not retrieving all the pages with your query, as it's unnecessarily expensive. I could be wrong about the type it returns vs. the type get_page_children expects, though. I actually strongly suspect that there might be a better way entirely to do this, and may think on it some more.

Topic Closed

This topic has been closed to new replies.

About this Topic