Sometimes you might want specific pages to be displayed on your site. If you want to display specific posts you can place them into the same category and loop though them by cat. However, you can’t do that with pages. In order to solve that, this is what you have to do if you want to loop through specific pages:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $args = array( 'post_type' => 'page', 'post__in' => array(34,56,7,22) ); $my_specific_pages = new WP_Query( $args ); while ($my_tile_pages -> have_posts()) : $my_tile_pages -> the_post(); the_content(); // ADD YOUR OWN CONTENT endwhile; wp_reset_postdata(); |
Copy this code where you would like to use it and in the assignment of variable $args modify the array with the IDs of the pages you want to use. In case you don’t know how to find your page ID, simply open up the editor for the Page you want. In the URL area you will see the page number mentioned in the http string. It will look something like this: ‘post=74’. That is your page ID number. Check out this guide for a visual on how to do this.
Next, add your own content inside the loop. Don’t forget to close out your php tags if your content will include html. For example:
1 2 3 4 5 | while ($my_tile_pages -> have_posts()) : $my_tile_pages -> the_post(); ?> <div class="main-content"> <?php the_content(); ?> // ADD YOUR OWN CONTENT <?php endwhile; wp_reset_postdata(); |
That’s it really. Here is an example of the code of a loop through specific pages that i created to make a mosaic of images that link to specific pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php $args = array( 'post_type' => 'page', 'post__in' => array(67,59,69,65,61,63,57,54) ); $my_tile_pages = new WP_Query( $args ); while ($my_tile_pages -> have_posts()) : $my_tile_pages -> the_post(); ?> <div class="box vertical-esp"> <div class="innerBox" style="background: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>) no-repeat; background-position: 50%; background-size: cover;"> <div class="titleBox"> <article id="post-96; ?>"> <a href="<?php echo get_permalink(); ?>" rel="bookmark"><h3><?php echo get_the_title(); ?></h3></a> </article><!-- #post-## --> </div> </div><!-- .innerBox --> </div> <?php endwhile; wp_reset_postdata(); |
Enjoy, and if you have any questions, leave them in the comments below.