Home Scripts PHP WordPress Preview Script

Who's Online

We have 1 guest online

Login Form



WordPress Preview Script Print E-mail
Scripts - PHP
by Jack Beltane (© Jack Beltane. All rights reserved.)   
Saturday, 06 March 2010 03:43

A friend wanted to show his most recent posts on a different page, outside the Word Press blog directory, so I cobbled together a dirty little script. It shows the whole last post, plus previews of the four posts before that. Thought you might enjoy it, too. Just copy-n-paste it into your html (don’t forget to change the extension of your page from "html" to "php").

Note: WordPress uses semi randomly generated table names, so you’ll need to go into your SQL tables and find your random value. If there isn’t one, leave the $tblvar variable blank (remove the underbar, too).

/*
you really should hide these SQL variables in a page that’s not accessible by the public,
then call it like this: include("../variables.php");
Put it somewhere that’s not in your root domain, but rather at a higher level on your server.
*/
$mysql_username="YOUR_USERNAME";
$mysql_password="YOUR_PASSWORD";
$localhost="YOUR_DATABASE_ADDRESS";
$dbname="YOUR_DATABASE_NAME";
$link = mysql_connect($localhost,$mysql_username,$mysql_password) or die ("Unable to connect to SQL server.");
mysql_select_db($dbname,$link) or die ("Unable to select database.");
// okay, you can stop hiding stuff now.

// main variables
$tblvar="abc123_"; // This is the random part of your SQL tables’ names — make sure you leave the final underbar!
$lim = 5; //the number of posts you want to show
$chars = 100; // the number of characters of the post body to show
$show=1; // the number of full posts to show
$path="http://path/to/your/blog"; // do NOT include the last / !

$wp_posts = "wp_" . $tblvar . "posts";
$new_news = mysql_query("SELECT ID, post_author, post_date, post_title, post_content FROM $wp_posts ORDER BY ID DESC LIMIT 0,$lim");

$i=1;

if ($new_news) {
while ($row = mysql_fetch_row($new_news)) {
$id = $row[0];
$author = $row[1] ;
$date = $row[2] ;
$title = $row[3] ;
$content = $row[4];

if ($i>$show) {
$blurb = $content." ";
$blurb = substr($blurb,0,$chars);
$blurb = substr($blurb,0,strrpos($blurb,’ ‘));
$blurb = $blurb."…";
}
else {$blurb=$content;}

$wp_users = "wp_" . $tblvar . "users";
$this_author = mysql_query("SELECT display_name FROM $wp_users WHERE ID=’$author’");
if ($this_author) {
$authrow = mysql_fetch_row($this_author);
$byline = $authrow[0];
}
else {$byline="unknown";}

$display_block .= ‘<a href="/v2/‘.$path.’/?p=’.$id.’">’.$title.’</a>

<span class="byline">posted by <b>’.$byline.’</b> (’.$date.’)</span>’.$blurb.’<p>’;

$i++;
}
echo $display_block;
}
else {
echo "Could not fetch news feed.";
}
mysql_close;

 

You must be a registered user to post comments. Register here.



Entire site contents are copyright © 2009-2010 by Jack Beltane. All rights reserved.
 


micro link to this story:
http://big-fly.net/?id=61


Poll

Breaking Benjamin...