treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress - Pages displaying out of order

  • I'm listing out all my wordpress pages and a little bit of their content using a foreach loop like the following:

    <?php
    $allPageIDs = get_all_page_ids();
    foreach($allPageIDs as $pageID) {
    $page_data = get_page( $pageID );
    $content = apply_filters('the_content', $page_data->post_content);
    $title = $page_data->post_title;
    ?>
    <div>
    <h2><?php echo $title; ?></h2>
    <div><?php echo $content; ?></div>
    </div>
    <?php
    }
    ?>
    However, there is a problem. They're displaying out of order. Since it's going by page id, it's going in the order that I created them, and not their set post order. Any Idea how I can fix this?
  • Have you had a look at get_pages()?

    It would be something like:
    $pages = get_pages();
    foreach ( $pages as $page ) {

    $content = $page->post_content; // I'm not sure if it's post_content. print_r( $page ); to see what you should use
    $title = $page->post_title;

    $output = '<h2>' . $title . '</h2>';
    $output .= '<div>' . $content . '</div>';

    echo $output;

    }