Show WordPress Post Titles & Posts On Your Website

If you use WordPress for your blog, but your blog is not your home page, you may wish to display the latest blog posts on your homepage, or any other page for that matter, to entice people into your blog.

WordPress Pages

If you use WordPress for your whole site (pages and all) then this is relatively easy to add the code in the code. It can be placed in your footer (where I have it), sidebar or anywhere else you want it. Unfortunately, as with most WordPress code, it cannot be pasted straight into a post or a page.

The code to show your latest 3 posts is:

<?php wp_get_archives(‘type=postbypost&limit=3′); ?>

Result:

The wp_get_archives code is hugely customiseable. It can show months, days, posts etc. The default code generated comes out as a list (<li>), but needs a <ul> tag around the php script.

To show, for example, the last 5 days where you have posted, with the post count next to it, you would use the following code:

<?php wp_get_archives(‘type=daily&limit=5&show_post_count=true’); ?>

Result:

One last example; If you would like to show Weekly posts for the last 7 weeks, with the word ‘Week:’ before each link and the words ‘was a good week’ after with each one in a <h3> tag, you would use the following code:

<?php wp_get_archives(‘type=weekly&limit=5&format=custom&before=<h3>Week: &after= was a good week</h3>’); ?>

Result:

That single line code, as you can see, can be adapted to show exactly what you want. More details about the wp_get_archives reference, can be found in the WordPress Codex. For hard coding the wp_get_archives html, it can be found on line 818 in the wp-includes/general-template.php

Non-Wordpress Pages

You can also include your latest blog post titles on your homepage, even if your pages are not generated within WordPress. (For example, my WordPress installation is located at www.mikestreetmedia.com/blog but I want my latest blog title to appear on my homepage, outside of the blog folder). It does not require complex database connections, but rather a call to the wp-load.php file.

To use the following code there are a couple of requirements:

1) The WordPress installation is located on the same server as the file where the code is going (for example: in a /wordpress/ folder)

2) Your page is a php page. If it is .html then rename to .php. unfortunately, you will need to upload the file to a server (or set up a local testing server) to make sure the code works.

Firstly copy the following code into your page:

<?php
// Include WordPress
define(“WP_USE_THEMES”, false);
require(“./blog/wp-load.php”);
query_posts(“showposts=3″);
?>

The ‘false’ tells the code not to include your template, the following line says where the wp-load.php file is. If your blog is in a /blog/ folder, you would change /Wordpress/ to /blog/. Finally the query_posts() line allows a variety of customisation. For example: if you wanted to show the 3 most recent posts in category 3, you would use the following code:

query_posts(‘cat=3&’showposts=1′);

Now you have called your posts, all you need to do now is include the loop and the information you want. The code is just the same as you would find in your index.php, or archives.php in your theme:

<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_centent(); ?>
<p><a href=”<?php the_permalink(); ?>”>Read more…</a></p>
<?php endwhile; ?>

This can be customised as you would when editing your theme.

Alternatively, you can use a website like Feederr, which will turn your latest blog post into an image, which updates each time. It walks you through step by step to get your box, which, when customised, looks something like this:

Mike Street media latest blog entry





Back to the Blog

No Comments Yet


There are no comments yet. You could be the first!

Leave a Comment



Back to Home