Simple Page-of-Posts Solution for WordPress
Recently I have been helping Make a Difference Wisconsin, a local non-profit, with their WordPress website. Without going into details as to why, I needed to create a simple ‘page of posts’ for menu hierarchy reasons. It needed to be a page, but I wanted it to display all of the posts from a particular category. Google took me to many solutions, including the WordPress Codex, but none of them worked. Some used out of date or deprecated query functions and some didn’t work with the custom theme that was already in place. This should have been a very easy task, but took me several days to pin down my solution.
So, if you want to create a simple page-of-posts that should work in any theme (with some minor tweaking, of course), feel free to use this code:
<php /* Template Name: pageofposts */ /* Author: Alex Glover Date: 4-Nov-2012 */ ?> <?php get_header(); ?> <div id="content-area"> <?php global $post; $args = array( 'numberposts' => 5, 'category' => 47 );//CHANGE THESE VALUES AS NEEDED $myposts = get_posts( $args ); foreach( $myposts as $post ) : setup_postdata($post); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><br /> <?php if ( has_post_thumbnail()) : ?> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?> " style="float:left; position: relative; margin: 0px 20px 0px 0px;"> <?php the_post_thumbnail('thumbnail'); ?> </a><p style="width:450px; float:left; position: relative;"> <?php the_excerpt(); ?> </p> <br /><br /><br /> <?php endif; ?> <?php endforeach; wp_reset_postdata(); ?> <div></div> </div> <!-- #content-area --> <?php get_sidebar(); ?> <?php get_footer(); ?>
Here’s a quick preview of what the ‘page of posts’ code looks like, but obviously aspects of this will be very different based on the theme that you are using.
You can check out the full page and see the code in action at http://www.makeadifferencewisconsin.org/investment-conference/past-conferences