I’ve got a client who has a very nice website which was built quite a while back. Since launching, they wanted to add a blog and a few other things to their site, for which we used WordPress. Also along the way, they wanted to show their most recent post on their home page, which is outside of “the loop” for Wordpress.
Now I’ve never claimed to be a WP pro, but I have figured out how to make this work. Below is the code to get it done. The trick is the first line, the required wp-load file. That’s what lets you use some of the wp functions outside of the loop.
<?php require( 'path_to_blog_root/wp-load.php' );
$getpostq=mysql_query('SELECT ID,post_title FROM wp_posts WHERE post_status="publish" ORDER BY post_date DESC LIMIT 1');
if (!$getpostq) { die('Error selecting info: '.mysql_error()); }
while ($getpostr=mysql_fetch_array($getpostq)) {
$post_title=$getpostr['post_title'];
$pid= $getpostr['ID'];
$postlink=get_permalink($pid);
echo "Recent Discussion: <a href=\"$postlink\" title=\"Link to $post_title\">$post_title</a>";
If you also want the post date, you can add these lines:
<?php require( 'path_to_blog_root/wp-load.php' );
$getpostq=mysql_query('SELECT ID,post_title FROM wp_posts WHERE post_status="publish" ORDER BY post_date DESC LIMIT 1');
if (!$getpostq) { die('Error selecting info: '.mysql_error()); }
while ($getpostr=mysql_fetch_array($getpostq)) {
$post_date=$getpostr['post_date'];
$post_date=date('F j, Y', strtotime($post_date));
$post_title=$getpostr['post_title'];
$pid= $getpostr['ID'];
$postlink=get_permalink($pid);
echo "Recent Discussion: <a href=\"$postlink\" title=\"Link to $post_title\">$post_title</a>%lt;i>($post_date)</i>"; ?>
By the way, if you have a better way to do this, I’d love to see it.

