Display Latest Post on Static PHP Page

Posted: August 4, 2010 in PHP
Tags: , , ,

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

Advertisements
Comments
  1. […] This post was mentioned on Twitter by Chinmoy Kr. Paul, Chinmoy Kr. Paul. Chinmoy Kr. Paul said: Display Latest Post on Static PHP Page: http://wp.me/pXe7U-34 […]

  2. Tony says:

    Hi, chinmoy29! I have a question about your coding. Is it possible to make full latest posts?
    Thanks in advance!

  3. mike says:

    How do you change the text it places ‘Continue Reading’? I’d like it to read ‘More…’

  4. Steph says:

    Hi,
    thanks for that,
    but unfortunately I can(t make it work..
    have got an error :

    /* Template Name: blip */
    Warning: require() [function.require]: URL file-access is disabled in the server configuration in /srv/d_TEAMS/www/www.tea.com/htdocs/wp-content/themes/atahualpa353/tst.php on line 8

    Warning: require(http://www.tea.com/wp-blog-header.php) [function.require]: failed to open stream: no suitable wrapper could be found in /srv/d_TEAMS/www/www.tea-ms.com/htdocs/wp-content/themes/atahualpa353/tst.php on line 8

    Fatal error: require() [function.require]: Failed opening required ‘http://www.tea.com/wp-blog-header.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in /srv/d_TEAMS/www/www.tea-ms.com/htdocs/wp-content/themes/atahualpa353/tst.php on line 8

    someone got an idea ..?
    Thks

  5. Deb says:

    Works beautifully! Exactly what I was looking for.

    Is it possible to change the length of the excerpt or is it simply pulling from what was written in the Excerpt section of the dashboard?

    • Deb says:

      Haha, nevermind about the excerpt length. Google to the rescue.

      Added this code to the functions.php file, where “20” is the number of words to display. (Default is 55.)

      add_filter(‘excerpt_length’, ‘my_excerpt_length’);
      function my_excerpt_length($length) {
      return 20; }

  6. Tony says:

    Chinmoy29 please help.

    Since I changed the path to my wp-blog-header.php for each new customized static page I’m getting the same title from “All in One SEO” Home title? And if I change home title they are changing all.. Is there a way to keep “All in One SEO” working normally for each new static customized page?

    Thank you for your time and help!

  7. mike says:

    i got it working except for it doesn’t show ‘read more’. or should it not be shown? please help 🙂

    thank you!

    • chinmoy29 says:

      use

      <?php the_content('Read More'); ?>

      instead of

      <?php the_excerpt(); ?>
      • mike says:

        hi chinmoy29,

        i tried using but ended with the whole post instead. i have since revert back to .

        any idea why is this happening? please help 😦

        • chinmoy29 says:

          may be you don’t use the

          <!--more-->

          tag. First goto ADMIN->POSTS->EDIT/ADD POST and put the

          <!--more-->

          tag after some paragraph(how much content you want to display first; that will be depending on your choice). After the

          <?php the_content('Read More...') ?>

          function would work.

          Cheers Mike 🙂

          • mike says:

            it works like a charm! thanks chinmoy29! one more question: is there a way to restrict the graphic/image from appear in the excerpt? i notice if i have my image first in my post, it will appear on my static page excerpt too.

            • chinmoy29 says:

              i notice if i have my image first in my post, it will appear on my static page excerpt too.

              Yes it will appear in your static page excerpt. But if u don’t want it so you can filter the image(means removes the image from excerpt). For that you need a customize code for it. You will create a function in your functions.php file and call that function from static page file

  8. Deb says:

    Me again. 🙂
    I just noticed that the updates show both new posts, and new pages that are created. Is there a way so it shows posts only and not newly created pages?

    Thanks!

    • chinmoy29 says:

      Sorry!! I am not able to understand your question. What you updated? What code your are using in your page file(php file for pages)?

      Page file(page.php) will show only page content not post content. If you customize the code and use query_posts()/WP_Query() function then the post would come there.

      • Deb says:

        Sorry, I’ll clarify. I’m using the code exactly as defined in your article.
        When I create a new post, an excerpt of the new post shows on my static home page like I want it to. But also when I create a new static page, an excerpt of that page is showing on my static home page just as if it were a post. But I don’t want new static pages to show like that, just new posts. Can that be changed?

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

    • chinmoy29 says:

      Arsalan,

      I think that your site is normal PHP site. There have no WordPress. R you install the WP for your Blog?

      This document is only for website not blog. Suppose you have a site http://www.example.com (using PHP) and a blog (WP) at this location http://www.example.com/blog/. Now in your website have a file x.php and you want to show post here from blog site. Then this functionality will be worked.

      Hope you understand.

  10. Chelsea says:

    Hey Chinmoy29,
    Thank you for this post! It was very informative 🙂
    However, is there a code to display comments as well (not a permalink to comments)? For example, you can use this code to permalink to comments, however can you have that information on the static PHP page?

  11. magpie says:

    My WordPress homepage uses a table to display various items. I’d like to place a Latest Post summary in one of the boxes. The page does not have a sidebar.

    Is this possible?

    • chinmoy29 says:

      Hi,

      Yes it is possible but don’t put the above code. This code is working on normal PHP website where blog is installed separately. You will use following code which i am giving at below:

      <?php 
      query_posts('showposts=1');
      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; ?>
      
  12. Hey,
    Thanks for the coding. When I use this, latest posts are shown in other pages too. How to avoid it?

    Regards,
    Anuradha.

    • chinmoy29 says:

      Thanks for using my code. Use conditional function like is_page(PAGEID) or is_page(PAGENAME). If have not more idea about this function then see the WP Codex for this function. You will get whole idea.

      Thanks
      Chinmoy

  13. Thanks for the coding. It really worked well for me.

  14. Jhon says:

    hi admin nice post but i have a question how can we make recent post in php page ?

  15. ansari says:

    nice information in php page my brother and my sister i like it

  16. mousa says:

    I copies and pasted your code in the static front page but it does not work, when i make Visit Site
    i see the code lines appear on the static front page, can you help me?

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s