Recent Posts
This is how I got my “Recent Posts” list appearing on my front page and sidebar for various pages.
I run the SEM Static Front Page plugin to provide the non-blog front page to this website, this website wasn’t originally planned as a blog, but as I’ve developed it, i’ve found the blog functionality to be better for some parts of the site and began using it more. It is certainly better then managing too many WordPress pages.
The code I used for generating the list I found in the old WordPress wiki, thanks to at http://wiki.wordpress.org/Recent Posts thanks to Nick Momrik.
Here it is:
global $wpdb, $tableposts;
$request = “SELECT ID, post_title FROM $tableposts WHERE post_status = ‘publish’ “;
if(!$show_pass_post) { $request .= “AND post_password ='’ “; }
$request .= “ORDER BY post_date DESC LIMIT $skip_posts, $no_posts”;
$posts = $wpdb->get_results($request);
$output = ‘’;
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$output .= $before . ‘<a href="’ . $permalink . ‘" rel="bookmark" title="Permanent Link: ‘ . $post_title . ‘">’ . $post_title . ‘</a>’ . $after;
}
echo $output;
}
Nick suggests adding it to the my-hacks.php file for older versions of WordPress, I use a plugin called “My Extras” which I stick functions like this one into. This makes it nice and easy to enable/disable the entire plugin when things go a little wierd on me (yes, it does happen!)
Creating a plugin like that is relatively easy, and there is plenty of documentation on it in the Codex
Next, I added some code to my Home page in WordPress’s Manage Pages.
<ul style=“list-style: none; font-size: .80em; color: #973131;”>
<h5 style=“font-size: 1.5em; margin-bottom: 5px; color: #973131;”>Recent Posts</h5>
<?php get_recent_posts(10, ‘<li style="margin-bottom: 5px;">‘, ‘<br />‘, 0); ?>
</ul>
</div>
This should be stylised and put in my style sheet, I will get to that another time, for just getting it working I tend to throw styles into the documents. Bad practice. Guilty as charged.
Next to the sidebar.php file.
I decided my front page should have the links in the content, but other pages will have the list in the sidebar. Instead of the list on the front page, I’m going to display a list of Categories and the number of posts in each.
<ul><li id=“pagenav”><h2>Categories</h2></li>
</ul><ul style=“line-height: 1.25em;”>
<?php wp_list_cats(‘list=1&children=1&optioncount=1&hideempty=1′); ?>
</ul>
<?php } else { ?>
<ul style=“font-size: .80em;”><li id=“pagenav”><h2>Recent Posts</h2></li>
</ul><ul>
<?php get_recent_posts(10, ‘<li style="margin-bottom: 5px;">‘, ‘<br />‘, 0); ?>
</ul>
<?php } ?>
I had all sorts of trouble using is_home() with the SEM Static Front Page plugin, so I decided to check for the post_name being home, which seems to work very well.
There you have it. A Simple easy way to get your recent posts list displayed on a page.