Display Recent Posts Outside Your Wordpress Blog Installation - PHP Script Formulation (Page 4 of 4 )
Now that you know the fundamentals of how to retrieve the top three latest posts from the wp_post table in a WordPress MySQL database, you are ready to write the PHP script. Below is the complete PHP script to retrieve the top three latest posts and present them as hyperlinks in the browser:
<?php
//connect to database
$username = "Put your Wordpress database username here";
$password = "Put your Wordpress database password here";
$hostname = "Put your database hostname here";
$database = "Put your database name here";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db($database,$dbhandle)
or die("Could not select $database");
//find the latest post ID with post status set to publish
$resultmax = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`='publish'") or die(mysql_error());
$rowmax = mysql_fetch_array($resultmax) or die("Invalid query" . mysql_error());
$maximum = $rowmax['max(ID)'];
//find the second latest post ID with post status set to publish
$resultmiddle = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`='publish' and `ID`<'$maximum'") or die(mysql_error());
$rowmiddle = mysql_fetch_array($resultmiddle) or die("Invalid query" . mysql_error());
$middle = $rowmiddle['max(ID)'];
//find the third latest post ID with post status set to publish
$resultmin = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`='publish' and `ID`<'$middle'") or die(mysql_error());
$rowmin = mysql_fetch_array($resultmin) or die("Invalid query" . mysql_error());
$low = $rowmin['max(ID)'];
//mysql real escape string to prevent MySQL injection
$maximum=mysql_real_escape_string(stripslashes($maximum));
$middle=mysql_real_escape_string(stripslashes($middle));
$low=mysql_real_escape_string(stripslashes($low));
//retrieve title tags for latest post
$resulttitlemax = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`='$maximum'")
or die(mysql_error());
$rowtitlemax = mysql_fetch_array($resulttitlemax)
or die("Invalid query: " . mysql_error());
$titlemax = $rowtitlemax['post_title'];
//retrieve title for second latest post
$resulttitlemiddle = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`='$middle'")
or die(mysql_error());
$rowtitlemiddle = mysql_fetch_array($resulttitlemiddle)
or die("Invalid query: " . mysql_error());
$titlemiddle = $rowtitlemiddle['post_title'];
//retrieve title for third latest title tag
$resulttitlemin = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`='$low'")
or die(mysql_error());
$rowtitlemin = mysql_fetch_array($resulttitlemin)
or die("Invalid query: " . mysql_error());
$titlelow = $rowtitlemin['post_title'];
//retrieve URL for latest post
$resulturlmax = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`='$maximum'")
or die(mysql_error());
$rowurlmax = mysql_fetch_array($resulturlmax)
or die("Invalid query: " . mysql_error());
$urlmax = $rowurlmax['guid'];
//retrieve URL for second latest post
$resulturlmiddle = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`='$middle'")
or die(mysql_error());
$rowurlmiddle = mysql_fetch_array($resulturlmiddle)
or die("Invalid query: " . mysql_error());
$urlmiddle = $rowurlmiddle['guid'];
//retrieve URL for third latest post
$resulturlmin = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`='$low'")
or die(mysql_error());
$rowurlmin = mysql_fetch_array($resulturlmin)
or die("Invalid query: " . mysql_error());
$urllow = $rowurlmin['guid'];
//display the most recent post as hyperlink in the browser
echo '<h3>Recent Blog Post</h3>';
echo '<ul>';
echo '<li><a href="'.$urlmax.'">'.$titlemax.'</a></li>';
echo '<li><a href="'.$urlmiddle.'">'.$titlemiddle.'</a></li>';
echo '<li><a href="'.$urllow.'">'.$titlelow.'</a></li>';
echo '</ul>';
?>
Implementation Tips
You will need to copy and paste the script into your sidebar PHP template in your main business website (not your WordPress blog); you can apply your own HTML style (color, font size, font face, header tags, etc) to match your template. Make sure the template uses a PHP extension. You will need to replace the database connection parameters with your own.
Below is a sample screen shot of the above script in action:
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by