Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress: How can I display multiple pages on one page?

Tags:

wordpress

Let's say I have three different pages, page1, page2, and page3.

I want page1 and page2 to display on my static front page. Do I restrict the loop to only pull page1 and page2, or do I need to start the loop, test for name="page1" or something like that, and then print? Thanks! -Joe

like image 906
Joseph Carrington Avatar asked Aug 20 '09 23:08

Joseph Carrington


2 Answers

I would skip the loop and simply use get_page($id) as described here:

http://codex.wordpress.org/Function_Reference/get_page

All you need to know is the ID's of your pages, and you can pull them one at a time anywhere you want.

like image 131
Miriam Suzanne Avatar answered Sep 22 '22 21:09

Miriam Suzanne


Here is an example of how you could do it. This code will work if you have all the pages that you want to be displayed under one parent. In this case, I was putting pages under the home page (p.post_parent = 2).

if ($post->post_type == 'page') {                    

    $pages = $wpdb->get_results("SELECT p.ID, p.post_name, p.post_title, p.post_parent, pm.meta_value FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS pm ON pm.post_id=p.ID AND pm.meta_key='wp_menu_nav' LEFT JOIN $wpdb->posts AS P ON P.ID=p.post_parent WHERE p.post_parent = 2 AND p.post_type='page' AND p.post_status='publish' ORDER BY p.menu_order ASC"); 

    if ($wpdb->num_rows > 0) {                          

        foreach($pages as $page) {

            //echo $page->ID . "<br>";
            $args = array( 'numberposts' => 1, 'post_type'=> 'page', 'include' => $page->ID, 'post_status' => 'published' );

            $myposts = get_posts($args);

            foreach($myposts as $mypost) {
                setup_postdata($mypost);
                echo the_content();
            }

        }

    }

}
like image 36
John Avatar answered Sep 23 '22 21:09

John