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



28 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.

  4.  

    This might be a stupid question, but I am new to WP and PHP.
    I don’t have this file in my directories:

    require(‘/home/username/public_html/blog/wp-blog-header.php’);

  5.  

    Never mind my previous question. I figured it out. Sorry :)

  6.  

    I’ve used a slightly modified version of this code. I’ve insert the code below directly on the static home page. It works great but I’m seeing the full post (content) in addition to the excerpt and title. How do I get rid of the content?

    <a href="”>

  7.  

    I’m not sure without seeing what code you have used. I would recommend using the code in the format I have shown above rather if you are having some formatting issues. Thanks

  8.  

    hello, i have successfully managed what you wrote here and thank you very much!
    but can it be merged with simplepe parser??
    as written on
    http://brightscape.net/blog/simplepie-parse-rss-atom-feeds/

    i mean just adding those codes after “< ?php.."
    and the rest to the body surely..

    thanks.
    ps. consider a non-php type'o'man :)

  9.  

    Hi s3e, I don’t think you can really merge the two, but you could quite easily use the SimplePie solution in my other blog post that you highlighted instead. You would simply put your blogs RSS feed into the SimplePie code and it will parse it, allowing you to configure how it’s displayed quite easily.

  10.  

    Hi, i have a few questions.

    1) Does your code replace the loop in the index.php ?

    2) What purpose does wp-header-blog.php serve ?

    Thank you for your time and effort and have a nice day :)

  11.  

    Hi,
    i am tryng to Do this but unfortunatly its not Working
    website.com/blog
    i am calling wp-blog-header.php

    like this:http://fogbreakmarketing.com/blog/wp-blog-header.php but its not working on my hosting

    it was working f9 whn i was tryng on localhost….Kindly give me some Advise

  12.  

    how about, i only want to display a single page ID in a static page?

  13.  

    Hi Fred

    In response to your questions:

    1. No, the code is purely for a non-Wordpress page, but usually it will be called index.php and will need to have the .php file extension in order to work correctly.

    2. wp-header-blog.php calls in the relevant external PHP includes in order to connect to the MySQL database, extract the post and then allow it to be rendered on the page.

    Thanks, I hope the code was useful for you.

  14.  

    Hi Arsalan

    You need to use the correct file path such as /home/username/public_html/blog/wp-blog-header.php and not a normal http file path. If you are unsure what your file path is, I’m sure your web host will be able to give you the information.

    Thanks

  15.  

    Wow, B!G Thank you for this.. i’v been searching it for a long time now.

  16.  

    @ gnobgnob I wrote a tutorial on excluding category from wordpress blog rss feeds that should point you in the right direction.

  17.  

    Hi,
    I have implemented the above code (or a version of it) and it is working fabulously! However, I have two columns and I want to add the 2 most recent posts. I want the most recent post to show up in column 1 and the other post in column 2. Right now, they are both showing up in both columns. Is there a way to specify which post goes where?
    Thanks in advance…

  18.  

    Hi Deborah

    I can see what you are trying to achieve. I’m not sure that this will be possible without some major tweaks to the functions.php file within WordPress. Basically, the way I would do it, is apply a div around the individual posts with a class, then float:left in the first and float:right on the second, rather than the current format, where the class is applied around both posts because of the nature of the excerpt function within WordPress. Another way to do this is with jQuery, but again it’s not an ideal solution.

    What you want to achieve is beyond the standard capability of the function from my understanding. It is certainly possible by teaking the functons.php file but I would recommend backing up your site before making any changes to the functions.php file in WordPress.

  19.  

    Hi Robin,

    Thank you for your reply. My client wants two different posts to show on the home page – actually he wants the first column to be the latest blog post and the second column to the latest post in a specific category — so for the second column I would need to specify the category . I really don’t know anything about wordpress (I’ve only just started working in it in the last week or so), although I am fairly comfortable working with php. Do you know what I would need to do to the functions.php file to make this work? If not, I guess I’ll have to figure something else out.

  20.  

    Hi Deborah

    What you could do is use the current format you have and change the setting to only show your latest WordPress post, instead of two. Then use SimplePie to pull in the latest post from a specific category by following this tutorial (and INCLUDE a single category instead of EXCLUDING it (using something like http://example.com/blog/feed?cat=102, instead of using minus (-) to remove the category). Then follow this tutorial on using SimplePie to parse the single category feed, and call that onto the page in the second column.

  21.  

    Hello Robin,

    Great post! How can I include the images that are included in a blogpost in the static page?

    Thanks so much for your reply!

  22.  

    There are various plugins available for this, but in my tests I found them to be unreliable and fiddly. You could try this plugin, which I have had some success with in the past though: http://wordpress.org/extend/plugins/thumbnail-for-excerpts/

  23.  

    Hello -
    Trying to add some blog excerpts to my static homepage using this format, however all of my pages are strictly through WordPress. My question is, instead of using the code:

    // Change path below to location of wp-blog-header.php on server
    require(‘/home/username/public_html/blog/wp-blog-header.php’);

    Is there a way to use an opposed to a direct path of .php on the server? (to link to blog posts page).

  24.  

    *correction* display an <a href link opposed to the .php file on server.

  25.  

    Hi Ashley

    I don’t know of any way to use a regular URL path. Generally you have to use a server path similar to the one in the example for it to work. If you aren’t sure of your exact path drop your webhost an email and they will give you the correct path. Once you know it’ll be very useful when you need to include filesi n this way in future.

    Thanks

    Rob

  26.  

    Can you use the code twice on 1 static html page to take from 2 different blogs i host on same server?

  27.  

    I don’t think that’s possible because the PHP hooks would be the same, even though you change the path in the head of the page. You could use SimplePie to pull in both posts rather than this method. I’ve written about that here: http://brightscape.net/blog/simplepie-parse-rss-atom-feeds/

  28.  

    Thanks Robin.

    This was just what i was looking for. Very much appreciated!!

Leave a Reply