How to add recent WordPress post exceprts on an external html page
25 of May2007
*** Update (Apr 4 08): I added a line of code that should strip out all HTML tags and formatting. So the resulting excerpt should only be text
Related Post: How to display 5 full posts…
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 from your recent post on your home page.
The solution? Use PHP and MYSQL to directly fetch the content from the WordPress database without even using WordPress.
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
//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
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id DESC LIMIT 1";
$query_result = mysql_query($query);
$num = mysql_numrows($query_result);
//close database connection
mysql_close();
//assign data to variables
$blog_date = mysql_result($query_result, 0, "post_date");
$blog_title = mysql_result($query_result, 0, "post_title");
$blog_content = mysql_result($query_result, 0, "post_content");
//$blog_permalink = mysql_result($query_result, 0, "guid"); //use this for 'p=11' format
$blog_permalink = $blog_url . mysql_result($query_result, 0, "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);
//how many characters should be shown?
$maxchars = 135;
//strip out the html tags, such as images, etc...
$blog_content = strip_tags($blog_content);
//cut down the size of the post to 135 characters
$blog_content = substr($blog_content, 0, $maxchars);
$blog_content = $blog_content . "...";
// 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) Find the “wp-config.php” file and the first three lines or so should contain your database name, user name, and password. Enter those in your file where I have ### for the db parameters. Just copy the names as shown in below. Do NOT CHANGE the “wp-config.php file”. WordPress will stop working if you do.
<?php
// ** MySQL settings ** //
define('DB_NAME', 'my_wordpress'); // 'my_wordpress' is the name of the database
define('DB_USER', 'my_user'); // 'my_user' is your MySQL username
define('DB_PASSWORD', 'my_password'); // 'my_password' is the password
3) Rename the file you are using to .php
4) Find a spot in your html where you want to display your date, title, and post excerpt. For example:
<div id="myId">< ?php echo $blog_date; ?>: < ?php echo $blog_title; ?> < ?php echo $blog_content; ?> <a href="<?php echo $blog_permalink; ?>">More </a> </div>
The above code will display the following:
May 25:
Show only the titles on the Archive Pages
Thanks to www.spiritfolk.com for this one. 1) Open up /wp-content/themes/default/archive.ph…
More
Orion
08/07/2007