Create A Page Of Posts In Wordpress

11 August, 2009 | Wordpress

A common practice when setting up a Wordpress blog might be to create a page and display posts in a certain category on that page. This causes some pages to act very much like category pages.

The first step in creating a page of posts is to create a page template that can be used by the pages. Create a file in your template directory called pageofposts.php and put the following comment in it.

<?php /** *Template Name: PageOfPosts */ ?>

This will cause it to be displayed in the Template drop down on your Wordpress page admin. This isn't going to do a lot so lets add some other functionality. Add the following lines to include everything you need to create a blank Wordpress page.

<?php get_header(); ?> <?php get_sidebar(); ?> <?php get_footer(); ?>

Between the get_sidebar() and get_footer() functions add the next code snippet. This basically constructs a query that will be used to get the correct posts from the database. The first if statement is used to detect which page the user has landed on. You will need to hand code this part, but it shouldn't be too difficult. Essentially, the is_page() function returns true if the value passed to it is the id of the current page. The $cat variable is then filled with an array containing the categories to show, this can be a single category or multiple. This is then added to an $args array, which is used to create a new WP_Query object after a simple if statement to detect if we are looking at a page.

if ( is_page('15') ) {   $cat = array(3); } elseif ( is_page('20') ) {   $cat = array(4); } elseif ( is_page('32') ) {   $cat = array(5, 7); } else {   $cat = ''; }   $args = array(   'category__in'     => $cat,   'caller_get_posts' => 1 );   if ( $paged > 1 ) {   $args['paged'] = $paged; }   $my_query = new WP_Query($args);

The $paged variable contains an integer of the page number we are currently looking at and is set up by Wordpress. Basically, if the $paged variable is greater than 1 we need to include this in the query.

One thing you can include here is to print out the contents of the page you are currently looking at. This following code prints this out.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2><?php the_title(); ?></h2>   <div class="entry">     <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>       <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>     </div> </div> <?php endwhile; endif; ?> <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>

Next we include some code to detect if any posts have been found, and if so, we loop through them and pick out the information for each post. This is a standard Wordpress "The Loop" and can be copied from any other page on your site. Just be sure that you use the $my_query object to call have_posts() as without it the function will return information about the page we are looking at and not the posts we are interested in. The functions have_posts() and the_post() simply run the same function for the global WP_Query object, which is contained in the $wp_query variable. Essentially, they are a short-cut to calling $wp_query->have_posts().

<?php if ( $my_query->have_posts() ) : ?>   <?php while ( $my_query->have_posts() ) :    $my_query->the_post(); ?>     <?php     //necessary to show the tags      global $wp_query;     $wp_query->in_the_loop = true;     ?>     <div <?php post_class() ?> id="post-<?php the_ID(); ?>">       <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>       <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>         <div class="entry">         <?php the_content('Read the rest of this entry &raquo;'); ?>       </div>         <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &raquo;', '1 Comment &raquo;', '% Comments &raquo;'); ?></p>     </div>     <?php endwhile; ?>    <?php else : ?>     <h2 class="center">Not Found</h2>   <p class="center">Sorry, but you are looking for something that isn't here.</p>   <?php get_search_form(); ?>   <?php endif; ?>

There is one thing missing here and that is pagination controls, commonly called next and previous links. This is because they require special consideration due to the fact that the normal functions involved in creating these links (next_posts_link() and previous_posts_link()) only look at the currently held WP_Query object, which in this case contains our page and not the posts we want to navigate through. Copy the following code into the previous block of code, just after the endwhile line.

<div class="navigation">   <div class="alignleft"><?php  if ( !$max_page ) {   $max_page = $my_query->max_num_pages; }   if ( !$paged ) {   $paged = 1; } $nextpage = intval($paged) + 1;   if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {   $attr = apply_filters( 'next_posts_link_attributes', '' );   echo '<a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', '&laquo; Older Entries') .'</a>'; } ?></div>   <div class="alignright"><?php  if ( !is_single() && $paged > 1 ) {   $attr = apply_filters( 'previous_posts_link_attributes', '' );   echo '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', 'Newer Entries &raquo;' ) .'</a>'; } ?></div> </div>

Upload all of this code to your content directory and you are ready to go. Every time you want to create a page that shows a bunch of posts just create the page using the Page Of Posts template and make sure that the if statement at the top of the file catches the page and selects the posts from the correct category.

This template is also a good example of how the WP_Query object works and how to create new queries on the page without overriding the currently retrieved items.

Written by Philip Norton.

Related posts:

4 Responses to “Create A Page Of Posts In Wordpress”

  1. Paul Brown |
    Hi Philip, Thank you very much for taking the time to put this post together, it is exactly what I have been looking for. Unfortunately my lack of experience with php means that I cannot get elements of your code to work in mine. My page of posts is loading fine here: http://www.yogawellbeing.co.uk/blog But as described above I have not been able to get paging to work. Wouuld you be able to show me how to implement pagination in the below code. (I realise the number of posts is limited to 10 at the moment - because of my pagination issue) Your help would really be appreciated! $cat, 'showposts' => $showposts, 'caller_get_posts' => $do_not_show_stickies); $my_query = new WP_Query($args); ?> have_posts() ) : ?> have_posts()) : $my_query->the_post(); ?> in_the_loop = true; ?> <div id="post-"> <a href="" rel="bookmark" title="Permanent Link to "> <?php the_tags('', ', ', ''); ?> <a href="#comments"> Not Found Sorry, but you are looking for something that isn't here. Thanks Paul
  2. Paul Brown |
    Hi Philip, Just tried submitting a comment with code - not sure if it worked as got no notification. Thanks Paul
  3. Melody |
    Hi, I want to thank you immensely! I am using a page of posts in the theme I am currently developing. I had been struggling to figure out pagination controls. I knew it had something to do with the WP_Query object but I am not proficient enough (yet) in PHP to come up with the solution myself. This was super helpful. -Melody
  4. Nicolas |
    Hi Paul, Thank you for putting this together. I have been struggling with templates for some time. I luckily stumble on it today. You've made this too easy for us. Usually I like to figure out stuff by myself. Believe me I would not ask if it was not urgent. I've been working at a template to show only one of my category on a page. I've got it worked out with a mix of your codes and someone else's. So far I have 10 posts in that category(research) and they all show up when I set $showposts to -1. However, when I limit the number of posts on page to 6, going to "older entries" just give me the same 6 latest posts instead of showing the previous 4 posts. Could you please have a quick look and let me know where I might be wrong. http://www.nicolasladouce.com/research. Thanks again for these codes above.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>