Formatting MySQL Output To XHTML In WordPress

“Often a simple query with some PHP does the trick just fine”

Here’s a funny story because I mentioned in the first post - WordPress Static Front Page Plug-in Question - that I’d used the Static Front Page plug-in for WordPress and in page.php I’d simply detected the page I wanted my actual weblog to be on and used MySQL to pull the posts out onto the page. This meant having a static home page as well as having a fully functional weblog.

Then I posted earlier tonight - Static Front Page Over Blog With WordPress - about the recommended cure in the WordPress Codex for this where you create a home page and put the WordPress installation into a sub-folder beneath it and nobody is the wiser. Kind of like I did with Hunter Island Press Inc. The cure works but wasn’t exactly what I was looking to achieve - or perhaps it was my obsessive self nagging me to find the cure I first sought.

So the only trouble I had was when on page.php I sucked out my $blog_content it wasn’t formatted to XHTML - no tags! OK so I actually found the cure and mistakenly thought it was only half the answer as it didn’t validate and I kept scouring here and there as I could be bothered for the rest of the answer. But of course it didn’t validate simply because I’d put my own temporary tags on the page earlier… laugh out loud or what! But I learned a lot scouring the include files and methods so it wasn’t a great loss.

OK here’s the drum… when you pull out the titles and dates via MySQL you can just whack your own tags around them but when you get to the content you’ll need something a bit smarter… so the method you’re looking for is:

wpautop($blog_content);

And its as simple as that from what I can see. In fact so simple I’d found it in the first five minutes of looking but unfortunately doofus me was doing a bunch of stuff simultaneously and had my own extra tags in there botching the validation side…

Oh well live and learn. I just find when I’m in a crunch its a lot easier to MySQL the information if at all possible and often a simple query with some PHP does the trick just fine. I’d have laughed when I found the solution if it wasn’t my own mistake too. Well it looks like I’ll be going with my original setup using the Static Front Page plug-in and a bit of fancy footwork after all.

3 Responses to “Formatting MySQL Output To XHTML In WordPress”

  1. Matt Robin Says:

    Guhhh - you’ve been busy man!

  2. nortypig Says:

    Hi Matt
    Not busier than usual but dragged myself away from blogging for a bit. Usually I just don’t seem to blog about what I’m doing, like most people, so there’s a world going on underneath nobody knows about.

    I think its very handy to put up solutions on the blog simply so I can find them again later as well. I did it a fair bit on my old hand coded blog as well but fell out of the habit.

    We all solve little problems every day and if we share the solutions we aren’t all reinventing the wheel unneccessarily.

    I think I like this summer thing, you’s were having your’s while I froze beside a snowy mountan on the other side of the world. Ha ha.

  3. Matt Robin Says:

    Trust me: It’s getting cooler again here now…(have only had one, light-frost so far though)

    It’s funny - but this article appears in the RSS feed as ‘Formatting MySQL Output To XHTML In Word’ - Word!!! Bloody hell! (Laughs)


Sitemap