How to add 5 full WordPress posts on an external html page
25 of August2007
Upon popular request I’ve decided to add another tutorial, similar to the previous one, on how to add several full blog posts on an external page.
*** Update (Apr 25 2012): I’ve written a new post for how to accomplish this without accessing the WordPress database directly (so it’s less of a hack). Take a look.
Well, here it is, and I’ll try to explain everything clearly.
Say you have a homepage that is not run by WordPress, and you just have your blog in the /blog/ directory or something like that. Yet you STILL want to display an excerpt 5 (or any number) of your most recent full posts from your WordPress blog on your home page.
1) Here’s the code you will want to write in BEFORE the Doctype (so the very first of your HTML):
<?php
//db parameters
$db_username = '###';
$db_password = '###';
$db_database = '###';
$blog_url = 'http://www.jamischarles.com/blog/'; //base folder for the blog. Make SURE there is a slash at the end
//connect to the database
mysql_connect(localhost, $db_username, $db_password);
@mysql_select_db($db_database) or die("Unable to select database");
//get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 5";
$query_result = mysql_query($query);
$num_rows = mysql_numrows($query_result);
//close database connection
mysql_close();
// html page starts after ?>
?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
2) Now, the text in the body will be a little bit different. Continuing where we left off…
Now, the problem here, is that we have dynamically generated content. That means we write a loop that goes through each table row in the database, gets the title, date, and text, then spits out on the html, and goes to the next row of the database and does the same thing again.
So if we use a div with same id, it’ll show that div up 5 times, each time with a different post. This is not acceptable, because it is not valid code, and could mess up the CSS. So we have to give it a class to be valid, or use tables. For this example we’ll use divs.
<body>
<?php
//start a loop that starts $i at 0, and make increase until it's at the number of rows
for($i=0; $i< $num_rows; $i++){
//assign data to variables, $i is the row number, which increases with each run of the loop
$blog_date = mysql_result($query_result, $i, "post_date");
$blog_title = mysql_result($query_result, $i, "post_title");
$blog_content = mysql_result($query_result, $i, "post_content");
//$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
$blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format
//format date
$blog_date = strtotime($blog_date);
$blog_date = strftime("%b %e", $blog_date);
//the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
?>
</body>
<div class="post"></div>
<span class="date"> <?php echo $blog_date; ?>:</code></span><br /><hr />
<a href="http://www.bluebreeze.net/blog"><?php echo $blog_title; ?></a><br /><br />
<?php echo $blog_content; ?> <br /><br />
<a href=”<?php echo $blog_permalink; ?>”>This Article</a> <br />
<a href="http://www.bluebreeze.net/blog">More Articles </a>
<?php } //end the for loop ?>
Related posts:
How to get WordPress on an external page without hacking the db
How to add recent WordPress post excerpts on an external html page
Just Good Clean Fun » Blog Archive » How to add recent Wordpress post exceprts on an external html page
08/25/2007