Display Latest WordPress Post on Static Page


Nov
25
2009

Wordpress logo If you have a mixture of static pages and WordPress-built pages on your website it can be quite useful to display an excerpt from your blog on a static page, for example your homepage.

The following guide shows you how simple it is to show an excerpt as well as control how much of the excerpt to display. To do this you will need to make sure the static page is a PHP page (I.e. has a .php file extension).

How to do it

1. Add the following PHP code to the top of your static PHP page:

<?php
	// Include WordPress
	define('WP_USE_THEMES', false);
	// Change path below to location of wp-blog-header.php on server
	require('/home/username/public_html/blog/wp-blog-header.php');
	// Change number below to show 1 or more post excerpts
	query_posts('showposts=1');
?>

The above code should be the first code on the page. Example shown below:

<?php
	define('WP_USE_THEMES', false);
	require('/home/username/public_html/blog/wp-blog-header.php');
	query_posts('showposts=1');
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<!-- HTML code continues... -->

2. Now you will need to add some PHP to the page where you want the excerpt to be displayed. An example is shown below:

<?php while (have_posts()): the_post(); ?>
<h4>Latest Blog Post on <?php the_time('jS F') ?></h4>
<p><strong><a href="<?php the_permalink(); ?>" title="Read full post"><?php the_title(); ?></a></strong></p>
<?php the_excerpt(); ?>
<?php endwhile; ?>

Now when you test your page you should see the latest excerpt from your blog on the static page.

Adjust Excerpt Length

If you want to control the excerpt further by showing more or less text you will need to add some code to the WordPress functions.php file. This file is located in siteroot/blog-folder/wp-content/themes/theme-name/functions.php or can be accessed from within the WordPress admin panel via Appearance > Editor > functions.php.

1. Within the functions.php file add the following code:

/* excerpt length for homepage */
function new_excerpt_length($length) {
	return 45;
}

Make sure the above code is pasted within the opening < ?php and closing ?> tags.

2. Adjust return 45; to increase or decrease the number of words shown in the excerpt before it is automatically truncated ([...]).

To see an example of these two pieces of code in action look at the Brightscape homepage or the About page.

  • Digg
  • Sphinn
  • Propeller
  • StumbleUpon
  • Twitter
  • del.icio.us
  • Facebook
  • FriendFeed

Related Posts



3 Responses to “Display Latest WordPress Post on Static Page”

  1.  

    Is there a way to do this from an rss feed instead of interacting with wordpress? That way it could be used regardless of who hosts the blog or what platform they use.

  2.  

    Hi Chris, just to clarify, you want to display a WordPress post/RSS feed item(s) from another site on your site, but you aren’t using WordPress on your site?

    If so, then I would recommend using another method to pull in the content. You could use SimplePie to parse the RSS feed, then you can choose exactly how to display the content on your own site. You can display one or more posts, and you can specify whether to just show a snippet. I wrote a tutorial on this here.

    An alternative method is possible using jQuery and a JSON feed. I wrote a tutorial on this here, however please note that the content will not be indexed by search engines as it is only added to the page once jQuery has loaded (I.e. loaded into the pages DOM).

    Let me know how you get on with it. Thanks

  3.  

    Thanks. The simplepie one looks good. And more important (since I’m just getting started with php) it looks like I’ll be able to implement it fairly easily.

Leave a Reply