Recent Wordpress Posts External PHP script
While redesigning the site I decided that I'd like to have the latest blog post titles linking to my blog on the main site. But how to do it?
After about 10 minutes searching on the web, I got lazy and gave up. Instead I gave the job to one of the programmers I use.
He came up within minutes with this great php script to do just that. Now whenever I add a blog post, my main site shows it.
This serves two purposes. It notifies people of the latest posts, but as importantly it means every time I update the blog, my main site gets updated and appears that much more "fresh" to the search engines - cool.
Important to note though that my blog is part of the site on the same domain. The script won't work if your blog and main site are on different domains.
Here is the script:
<?php
//MySQL Username
$user = "wordpress-blog-database- username-goes here";
//MySQL Password
$pass = "wordpress-blog-database-password-goes-here";
//MySQL Database Name
$database = "wordpress-blog-database-name-goes-here";
//Number of posts you want to have appear
$numOfPosts = 7;
//Setup connection
$mysqli = new mysqli("localhost", $user, $pass, $database);/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}$limit = $numOfPosts;
$sql = "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status IN ( 'draft', 'publish', 'future', 'pending', 'private' ) ORDER BY post_date DESC LIMIT $limit";
$result = $mysqli->query($sql);while ($row = $result->fetch_object()) {
echo '<ul>';
echo '<li><a href="'.$row->guid.'">'.$row->post_title.'</a></li>';
echo '</ul>';
}
?>
Once you have editted the script with your Wordpress Database details, and set the number of recent posts you want passed over, simply use a php include to where you want the results to be. Here is an example:
<?php include($_SERVER['DOCUMENT_ROOT'] .'/whatever-you-decide-to-call-this.php'); ?>
And there you go!
Enjoy,
Vic Carrara
Masterwebsoftware.com
Using the Comment Generator Script so you can comment and ask any questions about the Script ...
