Displaying WordPress Posts on a Static Page

One of the most frustrating things I tried to do with my WordPress blogs was to me something that should have been VERY easy. All I wanted to do was to show the latest blog post on my main site homepage. That is, I wanted to have a blurb with the post title, post date, and post promo text show up on a page EXTERNAL to my blog pages. My main website is written in ASP. All I wanted was for WordPress to "export" - just like it creates a RSS feed - a HTML feed to be used seamlessly by my ASP pages.

I looked for days and days for a solution. Nothing. Yes, I had a RSS to ASP conversion utility but it meant EVERY time my front page loaded that it had to sit there, read in the RSS file, and manually parse it out into ASP. That was silly! Why should I reparse a RSS file thousands of times in a day if the WordPress blog already knew the information? Was this really efficient? Apparently not - my server load was slowing everything down.

Finally, I dug into a manual solution. I decided to code ASP to DIRECTLY Access to MySQL WordPress blog and get the information directly. This would be quicker, more efficient, and more open to customization.

Here is how I got the latest post blog to show up on my static HTML pages!

Download the ODBC Drivers
I went onto the MySQL site and downloaded the ODBC driver. This is fairly easy. You run the install, you're done. Now ODBC on your server knows about MYSQL.

Create a System DSN
On the server, I went into Start - Control Panel - Advanced - and into the ODBC setup. I created an entry for my database. I gave it a name, and entered the username and password. It's fairly straightforward to set this up and test it out to ensure everything is working smoothly.

Create the ASP Code
This isn't hard, but just in case you don't work with ASP all the time, here is how the code works!

Just create this as a standalone file, for testing purposes. Call it something like "blog.asp". You'll want to include a standard adovbs ASP file to create your constant definitions for adOpenForwardOnly and so on.

(the first connect string should all be on ONE LINE)

strConnect2 = "ODBC;DSN=**DSNNAME**;Driver={MySQL ODBC 5.1 Driver};

Set BlogStr = Server.CreateObject ("ADODB.Recordset")
SQLText = "SELECT max(id) as mx from wp_posts where post_status = 'publish'"
BlogStr.Open SQLText, strConnect2, adOpenForwardOnly, adLockReadOnly, adCmdText
BlogID = BlogStr("mx")

SQLText = "SELECT post_title, post_date, post_name, post_content from wp_posts where id = " & BlogID
BlogStr.Open SQLText, strConnect2, adOpenForwardOnly, adLockReadOnly, adCmdText

response.write "<table bgcolor='#FFFFFF' cellpadding=8><tr><td><a href='**BLOGURL**'>"
response.write BlogStr("post_title") & "</a> - " & BlogStr("post_date") & "<BR>"

BlogContent = BlogStr("post_content")
BlogContent = replace(BlogContent, vbCrLf, "<BR>")

response.write BlogContent & "</td></tr></table>"

set BlogStr = NOTHING

Of course replace all the ** ** areas with your actual information :)

Give that file a test, and once you're sure it works, enjoy!

Creating an Active Blog - Main Page

Marketing your Small Business

Work from Home - Full Listing


Join Swagbucks!
You Can Get Free Gift Cards For Shopping, Searching and Discovering What's Online at Swagbucks.com