Display Recent Posts From Your WordPress Blog
Put it on your static home page
My main site is static since I am the only one who works on it. Just didn’t seem to be any point to creating in it WordPress. That doesn’t mean though that I don’t want some things from my blog showing up on my home page. I wanted the recent blog posts showing up in my sidebar, and after a bunch of searching (wasn’t quick like I thought it would be) I ‘kind of’ found what I was looking for, but had to modify it. If you are looking to do the same kind of thing though, it is actually quite easy. There are two small chunks of code that you need: the first is to open the database connection that your blog is linked to, and the second is to actually display the posts.
You don’t even need to know PHP!
Indeed you don’t, that is how easy this is. The first part is to put this code at the top of your page:
<?php define('WP_USE_THEMES', false); require('blog/wp-blog-header.php'); ?>
The ‘wp-blog-header.php’ file is not to be confused with your ‘header.php’ file that you use to put all the…well, the header information in your blog. The file you are really looking for is going to be in the root of your WordPress installation, whose path is probably going to be like it is shown in the example code above. Obviously change it to what you need it to be if it is different.
Here is the second part, which you would place wherever you want the ‘recent posts’ to display (usually the sidebar or footer):
<ul> <?php query_posts('showposts=3'); ?> <?php while (have_posts()) : the_post(); ?> <?php echo "<li>"; echo "<span class=\"date\">" ; the_date(); echo "</span>" ; echo " - "; ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php echo "</li>";?> <?php endwhile; ?> </ul>
The ‘ul’ I added because I wanted them to display in a list, so you of course do not have to add that. If you want to change the number of posts shown, it is pretty obvious which number to change (the ’3′ if you don’t). I then added ‘li’ tags around each one by echoing them out, and added a class of ‘date’ around the date code so I could target that seperately. Then you have the ‘a’ tag which wraps the title with the corresponding link, and the title code again to print out the actual title. And that is it! Pretty simple, but a very common thing web developers have to do. If you want to see it actually working, you can view it here, where I have a copy of my site redesign going.
With a little thought…
I have been told by some programmers that I work with, that the actual code that WordPress is built on is crap. If it is, I don’t care because of its ease of use. I don’t really know PHP, but by just looking at some sample code I can figure out how to do most of what I want in WP. This wasn’t that hard, I just didn’t like how deep I had to search to actually get what I wanted. I love the simplicity and ability to take this or that out of a WordPress site and stick it wherever you want. It is perfect for what I need to do. When I see stuff like this I think “Man, PHP doesn’t seem all that hard!” Of course I know this is example is really nothing. Besides, I’ve picked Ruby as my starting point so I can’t switch over. Maybe when I get that down, PHP will be a lot easier to understand. We shall see.