Home arrow Search Optimization arrow Page 4 - Display Recent Posts Outside Your Word...

TOOLS YOU CAN USE

advertisement

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:

blog comments powered by Disqus

SEARCH OPTIMIZATION ARTICLES

- Write Content For the Four Buying Personalit...
- Write SEO Content for Your Visitor`s Goals
- Title Tags: Not Just for Keywords Anymore
- The Challenge of SEO for Large Enterprises
- Viral Writing: the Beauty of Controversy
- The 375 Million Active Searchers You`re Prob...
- A Closer Look at Crushing Local SEO Competit...
- Crushing Local SEO Competition: A Case Study
- More Ways to Get Attention For Your Blog
- Cosmetic Surgery Marketing: Inbound Marketin...
- Dominating Local SEO
- Local SEO: Secrets to Killing The Competition
- Scientific Results Of 23 Million Visits: Cre...
- Feed Your Blog`s Readers Well
- Laying Out An SEO and Traffic Generation Str...
 
SEO Chat Forums  
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Contact Us 
Site Map 
Request Media Kit
Write For Us Get Paid 
SEO Weekly Newsletter
 
SEO Tools
Adsense Calculator
AdSense Preview
Advanced Meta-Tags
Alexa Rank Tool
Check Server Headers
Class C Checker
Code to Text Ratio
CPM Calculator
Domain Age Check
Domain Typos
Future PageRank
Google Dance
Google Keywords
Google Search
Google Suggest
Google vs Yahoo
Indexed Pages
Keyword Cloud
Keyword Density
Keyword Difficulty
Keyword Optimizer
Keyword Position
Keyword Typos
Link Popularity
Link Price Calculator
Meta Analyzer
Meta Tag Generator
Multiple Link Popularity
Page Comparison
Page Size
PageRank Lookup
PageRank Search
Robots.txt Generator
ROI Calculator 
S.E. Comparison 
S.E. Keyword Position 
Site Link Analyzer 
Spider Simulator 
URL Redirect Check 
URL Rewriting 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular SEO Chat Topics
All Tutorials & Tools
 
SEO Chat is sponsored by:
Close this Sponsor Message