Big Square Blog is a sandbox about web development, freelancing, and all things WordPress. It is written by Philip Arthur Moore, a traveler and web developer from Houston, Texas.

The Entry Archive

How to add a WordPress Mini-Loop to your static front page.

Update: I have come up with a much better solution to this problem. You can read about it by clicking here.

When I decided to make Big Square Dot a part static, part dynamic website, I was at a loss as to how I would go about incorporating my latest blog entry into the home page of Big Square Dot. I did not want to install WordPress into the root directory of www.bigsquaredot.com because I am all about organization and don’t much enjoy the sight of ‘wp-’ files littering my domain’s root folder.

Latest From The Blog Snapshot

What I did want was a simple way to include the date, headline, and excerpt of the latest entry from the Big Square Blog into the Big Square Dot main page, which is mostly static.

Enter Wordpress’ Mini-Loop and a little bit of trial and error.

The Setup

The WordPress Codex currently has a section up on adding a Mini-Loop to a static front page, but for my purposes, it was incomplete. The Mini-Loop tutorial only gives the code markup for how to include headlines of the latest blog posts on a static page, but I needed more, like the post date and the excerpt.

Before WordPress code can be executed on any static page, the following lines of PHP must be included in the header, above the body start tag.

  1. <?php
  2. define('WP_USE_THEMES', false);
  3. require('yoursite/wp-blog-head er.php');
  4. ?>

Note: There should not be a space in ‘wp-blog-header’.

Simple enough. This code will allow the execution of WordPress template tags (like the_date_xml(), etc.) on static pages. Because the only portion of my WordPress blog that I wanted to include on the Big Square Dot homepage was the latest post, I did not opt to include my blog header or sidebar into the static page, but this is also an option.

The Code

The original code to include post titles onto a static home page is located at the WordPress Codex repository.

I knew that I would need the post title, post date, and excerpt on the Big Square Dot homepage, but I also knew that I would not be able to use some of the more common WordPress template tags because I would not be employing the use of The Loop. After examining my WordPress database and adding some code to the original Mini-Loop, this is what I came up with:

  1. <?php
  2. $how_many=1;
  3. require_once("yourblog/wp-conf ig.php");
  4. $news=$wpdb->get_results("SELE CT `ID`,
    `post_title`,`post_excerpt`,`p ost_date` FROM $wpdb->posts
  5. WHERE `post_status`= \"publish\" and `post_type`=\"post\"
    ORDER BY ‘ID’ DESC LIMIT “
    .$how_many);
  6. foreach($news as $np){ ?>
  7. <p class="date"> <?php echo the_date_xml($np->ID)
    ?></p>
  8. <h3 class="title"> <?php
  9. print ("<a href=\"");
  10. echo get_permalink($np->ID);
  11. print ("\">$np->post_title</a>");  ?> </h3>
    <div class=”excerpt”> <?php
  12. echo the_excerpt($np->post_excerpt)  ; ?> </div> <?php
  13. } ?>

Note: There should not be a space in ‘wp-config’ or ‘post_date’. Note that the vertical line breaks are just for the sake of code visualization.

You can download this code (.txt version) to get a clearer look at how the code works on my homepage: WordPress Mini-Loop Code.

Important Notes

I have not tested this code out extensively with more than one post on the front page. In fact, one of the reasons that I decided to use this loop for one entry only was because setting the $how_many variable to more than one gave me major problems with both the post date and excerpt.

I was limited to using the the_date_xml() template tag and the the_excerpt() as both of these tags accepted the argument “$np->”.

The Big Caveat

This is extremely important. If you choose to use this code on a static homepage that does not have WordPress installed in the same directory, you need to make absolutely sure that you give each entry a manual excerpt.

If an excerpt is not manually entered for your latest entry, you will get the excerpt from the second to latest post, or whichever post you last gave a manual excerpt to. It’s a headache, I know. But it’s the only solution that I could come up with for the moment while I am still figuring out PHP/MySQL and how it interacts with WordPress.

I have not tested this code out with images, so that’s another thing you may want to consider when manually typing in excerpts instead of having WordPress generate them for each post.

Any additions or modifications of this code that will allow for multiple posts on a static page which is located outside of the WordPress directory would be appreciated and welcomed.

Another Huge Caveat

This technique will not work if you are using the wp-cache plugin.

Post Date: March 4, 2007

2 Responses to “How to add a WordPress Mini-Loop to your static front page.”

  1. Built From Skratch:

    February 18th, 2008 at 6:50 am

    Just wanted to say hello and great blog!

  2. Setsuken:

    May 1st, 2008 at 3:29 am

    Great and very useful post! Thank you for writing it.

    One question if I may though? Is there any way to include said headlines from only ONE category? :D

Leave a Reply

Quicktags: