Big Square Blog is a sandbox about web development, freelancing, and all things WordPress. It is written by Philip Arthur Moore, a traveler and web developer from Houston, Texas.

The Entry Archive

A (Smarter) Solution for Displaying WordPress Entries on ANY Part of Your Website

Latest From The Blog Snapshot

Last week I brought to you a post on how to add a WordPress Mini-Loop to your static front page. I didn’t realize it at the time, but after some thought, I must say that there were some serious problems with that solution. For starters, the static front page of Big Square Dot was running noticeably slower after implementing the Mini-Loop. Secondly, and most importantly, I could not use the WP-Cache plugin with the above code.

I knew there had to be a better way of including part of my WordPress blog on my static front page, and today it struck me. “Instead of calling on WordPress template tags to execute very simple PHP/MySQL functions on my front page,” I told myself, “I should treat my WordPress database like what it is: a database!”

A Recap of the Problem

My problem was as follows: I needed a way to include the latest post from my WordPress blog on my static front page, which is in no way related to my WordPress installation directory. I did not want to use anything other than standard ol’ PHP/MySQL to connect to my WordPress database, gather information, and then output it onto my front page. I would need the date of the latest entry, the title and permalink of the latest entry, and the excerpt of my latest entry.

The Solution to the Problem

I’m going to keep this pretty cut-and-dry. If you guys and gals have any questions, leave your comments and I will see to it that they get answered. Before I start, I would like to reiterate that the following solution should work on any page outside of your WordPress installation directory. The only thing that is involved is PHP/MySQL and your WordPress database. No template tags are needed.

First, we will configure our website database connection, connect to the database, and then select our WordPress database.

  1. <?php
  2. /*Turn off error reporting.*/
  3. error_reporting(0);
  4. //
  5. /*Configure your database connection*/
  6. $dbhost = 'localhost';
  7. $dbuser = 'dbuser';
  8. $dbpass = 'dbpass';
  9. //
  10. /*Connect to the database.*/
  11. $conn = mysql_connect ($dbhost, $dbuser, $dbpass)
  12. or die
  13. ('There was an error connecting to the database.');
  14. //
  15. /*Select your WordPress database.*/
  16. $dbname = 'dbname';
  17. mysql_select_db ($dbname);

Next, we will output our latest post from the WordPress database, showing the date, headline as a link, and excerpt of the post after getting our data.

  1. /*Get data from the database*/
  2. $howmany = 1;
  3. $result = mysql_query
    ("SELECT * FROM wp_posts
    WHERE `post_type`=\"post\" and `post_status`= \"publish\"
    ORDER BY post_date desc
    LIMIT " . $howmany);
  4. //
  5. /*Display the data*/
  6. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  7. //Date function
  8. $datevalue = $row['post_date'];
  9. $dateArray=explode('-',$dateva lue);
  10. // Example of results
  11. // $dateArray[0]= 2007
  12. // $dateArray[1] = 02
  13. // $dateArray[2] = 05
  14. //
  15. ?> <p class="date"> <?php
  16. echo date('F j, Y', mktime(0, 0, 0, $dateArray[1],
    $dateArray[2], $dateArray[0]));
  17. echo "</p>"; ?>
  18. //
  19. <h3 class="title">
  20. <a href="<?php echo "{$row['guid']}”;?>”
    title=”Permalink to: <?php echo “{$row['post_title']}”;?>”>
  21. <?php echo "{$row['post_title']}”;?></a>
  22. </h3>
  23. //
  24. <div class="excerpt">
  25. <?php echo "{$row['post_excerpt']}”;?>
  26. </div>

Finally, we will free our memory and close off our WordPress database connection.

  1. //Free the memory and then
  2. mysql_free_result($result);
  3. //Close the database connection.
  4. mysql_close ($conn);  ?>

Download the Source Code

It will make things a lot easier on your eyes if you download the source code (.txt format) and view it in Dreamweaver or a comparable application.

Study the code and you’ll see that it’s easy to change things around. You can include more than one post on a static page by altering the $howmany variable in the script and you don’t have to worry about dealing with WordPress template tags. Just make sure that you fill in your optional excerpt box for each post and everything will go smoothly.

Also, make sure that you configure the script properly. In your WordPress wp-config.php file there will be all the variables you need to properly configure your database connection if you have forgotten the settings.

Let me know if you have any questions and I will do my best to answer them. Keep in mind that I am a novice when it comes to PHP/MySQL so be gentle!

Post Date: March 12, 2007

60 Responses to “A (Smarter) Solution for Displaying WordPress Entries on ANY Part of Your Website”

  1. Nick Jackson:

    March 16th, 2007 at 1:16 pm

    Hi - I’ll start by saying ‘great site’, really interesting content, and it looks lovely. I have used an alternative solution to this issue - a bit of code supplied by SimplePie, which reads the rss feed and displays the links to the blog entries on the home page. I have no idea which is more elegant, but in the interest of educating myself I might give your solution a go.

  2. Philip Arthur Moore:

    March 16th, 2007 at 1:39 pm

    Up until your post, I had never heard of SimplePie. I’m taking a look at it right now and I’m liking what I see! I may have to give this a shot. Thanks for the comment!

  3. David Kornahrens:

    March 19th, 2007 at 9:37 pm

    I’m actually really impressed with this blog. I’ve checked it out a few times while designing. It’s come up a few times in random conversations on the net. Impressive. Keep up the good work.

    David Kornahrens
    DropJawMag.com

  4. Philip Arthur Moore:

    March 20th, 2007 at 2:08 pm

    Hey David,

    Thanks a bunch for stopping by and commenting. I took a look at your website and also enjoy it a lot. I’ll make sure to keep up with you and your work. Thanks again!

    Philip

  5. Alex:

    March 31st, 2007 at 12:53 am

    I’m such a Wordpress noob and their forums are pretty much useless when it comes to asking for help. You seem pretty well-versed in this whole area, so I was wondering if you could help me out. I have a website with three categories displayed on the page in three different div containers. News, Shows, Misc.

    I want them to each show up on the same page. If I go in and change News, I want the news section to be updated. If I update the Shows section, I want that to be updated.

    I’ve been reading about loops and all this other non-sense that I can’t get a grasp of. I’ve heard that I need multiple loops, one loop, all sorts of loops and I’m just flat out lost.

    This is sort’ve like what you’re doing, except with a few more categories and posts. Would you mind helping a very lost and frustrated wordpress guy? Thank you.

  6. Philip Arthur Moore:

    March 31st, 2007 at 8:40 pm

    Hey Alex,

    What you will need to do is use the query_posts function in WordPress. I will write a post about this and publish it on Monday if you can wait on it. Otherwise, click on the link that I have included and it should give you a good idea on how it works.

    Cheers.

  7. beth:

    April 24th, 2007 at 6:31 pm

    I’m trying to do this, but within the Wordpress directory. Basically I’ve configured a static page in Wordpress. By default the page template has the loop. However, instead of displaying my most recent entry it displays the content of this page as an entry, and only that. Any help would be most appreciated!

  8. Jon:

    April 25th, 2007 at 10:42 pm

    Hi!

    Thanks for the tutorial :) I have two newbie questions:

    1- For which file outside of the WP directory am I actually adding this code?

    2- I’m having a very difficult time figuring out how I can “feed” my postings to my already existing site. I thought maybe your technique would work… if there is a way to post the whole blog instead of just the mini-loop?

    Sorry for the n00b questions, just trying to figure it all out!

    Thanks

  9. Jennifer:

    May 9th, 2007 at 8:18 pm

    Thanks - this is just what I was looking for. I understand the code you created but I was way too confused to know where to start.
    Here is where I used it: http://www.davisagency.ca. Now I have to figure out how to add a bit of space between the thumbnails and words.

    Thanks a bunch.

  10. Heidi:

    May 31st, 2007 at 4:10 pm

    Thank you so much posting the code, this is exactly what I have been looking for. I run two sites, one is a wordpress blog, and the other isn’t, so I wanted to be able to include the posts. Thanks Again!!!

  11. Kent Schnepp:

    June 5th, 2007 at 9:44 pm

    Excellent post. This is exactly what I have been looking for. Thanks!

  12. Germán W.:

    June 12th, 2007 at 8:56 am

    Is there a way i could use the “the_excerpt” tag so i get the first 55 words auto? Thanks!

    PS: i works great anyway, very simple too!

  13. Bridget:

    June 20th, 2007 at 2:00 pm

    Super. Thanks, just what I was after :)

    Quick question - would it be easy to hack this so it pulls out only a specific category? I’m just thinking it would be useful to use as something similar to “asides”…?

  14. David Rooker:

    June 28th, 2007 at 10:13 pm

    Thanks for posting this. It is doing exactly what I wanted it to! Simple, neat, and more php than I know how to create.

  15. Daniel:

    July 3rd, 2007 at 4:01 am

    Hey

    Nice code…but i get no result when it is published. Just a blank page!

    I have MySQLversion: 5.0.41, i think that got something to do with it. I could really need som help. Thanx

    //Daniel

  16. Keely:

    August 6th, 2007 at 4:12 am

    Hi,

    Love the website: visual + content; I’m subscribed to your feed. Had a look at that “DropJawMag.com” - bloody awful website - just the way we females want to be portayed (not).

  17. her divine shadow - all in one place » My del.icio.us bookmarks for August 11th through August 15th:

    August 15th, 2007 at 9:13 pm

    [...] Big Square Blog » A (Smarter) Solution for Displaying WordPress Entries on ANY Part of … - tags: wordpress blog [...]

  18. Pete White:

    August 24th, 2007 at 2:31 am

    Hi,

    I was just wondering if there was a way to display a count of the comments as well?

    Any help is much appreciated, thanks.

  19. Simon:

    September 6th, 2007 at 5:32 am

    Great post, has worked a treat for me.. however, the following code:


    <a href="<?php echo "{$row['guid']}”;?>

    Doesn’t seem to link to the individual post page? Is this right or have I implemented something wrong?

  20. Simon:

    September 8th, 2007 at 6:14 am

    Apologies, ignore my last comment, it works perfectly.

    Is there a way you can adapt the code to only pull in posts from a certain category?

  21. Alex:

    September 27th, 2007 at 10:06 am

    That’s fantastic. I was looking for a solution like this for weeks!!. Good job. I have runnig the code into a post with exec-php plugin and works fantastic. You can see it athttp://www.PCasalvo.com
    Thanks

  22. MonkeyPug » Blog Archive » Mini-Feed:

    September 29th, 2007 at 1:59 pm

    [...] blog. Must say a huge thank you to Philip Arthur Moore at bigsquaredot for his really helpful post A (Smarter) Solution to Displaying Wordpress Entries on Any Part of Your Website. The php snippet he provides works a [...]

  23. Marcel:

    September 29th, 2007 at 7:28 pm

    This is the type of code i’ve been looking. I have no knowledge on how PHP works.(I know what it’s suppose to do, but not how to implement it)
    I made the code you supplied a php file, updated the database info to match my WP-config.php. but then how do I make my html call it? or do I just put all this code in my html where I want that data to appear?

  24. Philip Arthur Moore:

    September 29th, 2007 at 7:43 pm

    …but then how do I make my html call it? or do I just put all this code in my html where I want that data to appear?

    Marcel, this code will only work on pages with an extension of .php. You need to put the code in your file where you’d like the data to display. That’s all there is to it, really. I hope this helps.

  25. Marcel:

    September 29th, 2007 at 8:46 pm

    Thanks a lot. Got it to work beautifully. I have a drawing & sketches blog, now to figure out how to add the image from the post into my main page.

    Thanks for the code again

  26. Allison:

    November 5th, 2007 at 10:23 am

    This is perfect for my needs! Thank you for the help.

    I have one question. I would like to include the last few entries in their entirety on the static front page, rather than just an excerpt. Can you tell me how to accomplish this? Thank you!

  27. Jamie:

    November 5th, 2007 at 9:12 pm

    Philip,

    Thank you for this excellent post. I’m new to using Wordpress and would like to accomplish what you are doing — have about four of my post show up on my static home page. This is so much more in depth than the WordPress tutorials.

    I’ve tried your code but I am getting an error. I know it is something I’m doing wrong. Also, like Allison above, I’d like to have the entire post show up. Can you please assist a technically frustrated WordPress newbie? Any help would be greatly appreciated. Thank you.

  28. elliot:

    November 11th, 2007 at 2:00 pm

    This is a really nice solution to my problem. Thanks.

  29. vincent:

    November 12th, 2007 at 10:57 am

    Hey, I love your code it works wonderfully,
    just one question, the excerpts don’t have spaces between lines and paragraphs, even when I put them in when typing out the excerpt in the wordpress admin section. They all just show up together in one huge long paragraph. How can I make it so that the excerpts show up the way I write it, for example,

    If I write:

    “Hi there!
    My name is Bob”

    It shows up as:
    “Hi there!My name is bob”

    How do I fix this problem? any help is much appreciated thanks!
    Again, I love the script!
    Vincent

  30. Philip Arthur Moore:

    November 12th, 2007 at 5:26 pm

    Hi Vincent,

    Try coding line breaks into your excerpt box.

    For example:


    <p>"Hi there! <br /> My name is Bob."</p>

    Cheers,
    Philip

  31. Vincent:

    November 14th, 2007 at 7:26 pm

    Hey Philip,
    Thanks for the reply, it works perfectly. Thank you so much for writing this script!

    Have a good 1!
    Vincent

  32. Pauline:

    December 28th, 2007 at 6:40 pm

    Hi Philip, I’m new to using Wordpress and would like to how how to incorporate the variables onto the code that you have include on this page. What I want was to have the 5 of the latest titles of my post showing up on the home page http://www.westernstatesgeneti cs.org from http://www.westernstatesgeneti cs.org/wordpress?
    I have tried but just could not figure out how to do that with the code that you provided. Can you please provide me more directions? Thanks and I really enjoy your site.

    Pauline

  33. David Zemens:

    December 29th, 2007 at 8:35 am

    Thanks for the inspiration. Using your code and tweaking it to my needs, I created a “Featured Post” area at the top of my main WordPress index page template. Works great!

  34. Ryan Ward:

    February 18th, 2008 at 7:55 am

    I’ve been trying to do this on my test blog and haven’t found a good solution and this one seems a little too advanced for me. How about just adding an rss reader to pull your latest post into a section of the static page in a div?

  35. Philip Arthur Moore:

    February 18th, 2008 at 7:59 am

    @Ryan: Putting in an RSS feed is certainly an option, but the issue that I have run into with RSS feed based content aggregation is a lack of control with regard to how the content is displayed. Using the code in this post, I am able to display the content exactly how I would like it to be displayed. But, doing the RSS method is definitely a solution.

  36. Ryan Ward:

    February 18th, 2008 at 8:14 am

    Phillip,

    Thanks…I got your code to work - http://www.realestatetestblog. com. This is my most adventurous theme. I’ve got suckerfish dropdown menus and now I need to figure out how to call 2 pages in a similar way as the blog post in the 2 div boxes at the bottom of the page…wish me luck!

  37. Ryan Ward:

    February 18th, 2008 at 8:38 am

    Phillip,

    I just noticed that I’m not getting an excerpt to show. Does something need to be added beyond the code given in your example?

  38. Richard Knop:

    March 10th, 2008 at 3:26 pm

    Hello,

    Just wanted to thank you for this info. I have been trying to google something like this for days already (I’ve started to make my own wp themes recently and one theme required top show only the last post with excerpt outside of the wp loop)… Once again thanks.

    By the way, your blog looks great. And what is more important, you got really great content here (also started my own blog recently… but it’s just few days old… just few articles there).

    Keep up the good work. I’ll be checking this blog more often from now ;)

    Richard

  39. Daniel:

    March 10th, 2008 at 6:41 pm

    Hi. I know from earlier you wanted to not have to worry about posting an excerpt. Well using the code you already have replace your excerpt information with this:

    Change the 100 to any number you’d like. It takes the post content and just trims off the end and adds the ellipsis. This way you don’t have to worry about posting an excerpt.

  40. Daniel:

    March 10th, 2008 at 6:42 pm

    Here’s the code it got cut-off last time.

    $content = substr($row['post_content'],0, 100);
    echo $content . “…”;

  41. Mastering Your WordPress Theme Hacks and Techniques:

    March 16th, 2008 at 7:43 pm

    [...] Displaying WordPress Entries on static pages- A simple solution to include the date, headline, and excerpt of the latest entry from your Blog [...]

  42. Schweizer Wordpress Magazin » Anleitungen » Feeds mit PHP und MySQL auslesen:

    March 17th, 2008 at 2:16 pm

    [...] Gefunden bei: A (Smarter) Solution for Displaying WordPress Entries on ANY Part of Your Website [...]

  43. paidnetpedia.com » Blog Archive » Hacking WordPress Themes:

    April 12th, 2008 at 4:53 pm

    [...] WordPress template and make linked images appear instead of text for your categories in posts. 11) Displaying WordPress Entries on static pages- A simple solution to include the date, headline, and excerpt of the latest entry from your Blog [...]

  44. SEO & Web Design » Blog Archive » Mastering Your WordPress Theme Hacks and Techniques:

    May 6th, 2008 at 1:32 am

    [...] Displaying WordPress Entries on static pages- A simple solution to include the date, headline, and excerpt of the latest entry from your Blog [...]

  45. ed:

    May 24th, 2008 at 9:04 am

    Hi what modifications I have to make it order for the output to display exactly as the main blog page? Mine is only showing the title and date

    http://greenarchers.ph/display -wordpress-post.php

    I want to have the same details as shown in the blog page, with author, category and number of comments
    http://greenarchers.ph/wordpre ss/

    How do i change the font as well?

  46. ed:

    May 24th, 2008 at 9:26 am

    nice

  47. ed:

    May 24th, 2008 at 9:50 am

    Is there also a way to show only posts from a sepecific category?

  48. Cristobal:

    May 29th, 2008 at 3:49 am

    i’m going carzy. i have tried everything starting for doing what you ‘ve said here. the result is always the same: my index page outside the WP folder (one level up) and the place where the posts should be is empty. nothing is displayed. i change database for WP to an existing one. everything ok with that, i was able to login to blog and displayed where the WP folder index is, but as far as the trick of your code goes, nothing happend. blank blank blank.
    any clue?

  49. Keith:

    May 30th, 2008 at 4:41 pm

    Thanks! I’ve been trying to do this for ages. Great post.

  50. cristobal:

    May 30th, 2008 at 11:22 pm

    Nothing yet? Blank page problem….

  51. cristobal:

    June 4th, 2008 at 9:04 pm

    Ok, since nobody answers, for the mental sake of everyone who suffer the “blank page” problem (i almost went nuts) this script works for WP install version 2.3 and below. i tried other instalations (2.5) and the “guid” column in the “wp_post” table were always empty, and the “post_type” column same thing, therefore, nothing shows on the index outside WP folder, since those columns are conditionals in the script in order to show the freaking post. Feew!

    peace and good luck.

  52. Philip Arthur Moore:

    June 5th, 2008 at 10:49 pm

    Sorry cristobal,

    As you may have already been able to tell, posts on BSB have been few and far between during the last year. I’ve been up to my neck in projects and I’m also working on a redesign of the site and also a revised version of this post.

    Cheers,
    Philip

  53. Justin:

    June 10th, 2008 at 11:43 pm

    Thanks cristobal!

    This was driving me nuts.

    Philip… looking forward to you 2.5+ fix.

  54. badger:

    June 21st, 2008 at 1:28 am

    I am currently using wordpress 2.5 and this code works for displaying my most resent post(s) on my static homepage, but with one problem. It pulls in date and title (with permalink), but does not get the excerpt? has this happen to anyone else?

  55. badger:

    June 21st, 2008 at 2:29 am

    well don’t i feel stupid! forgot to fill in my excerpt fields before i publish my posts. Thanks a million!

  56.   Table Of Contents Of Wordpress Tutorials, Helps, Tips and Tricks by aComment.net:

    July 15th, 2008 at 12:36 am

    [...] Displaying WordPress Entries on static pages [...]

  57. Paul Anthony:

    August 30th, 2008 at 6:42 pm

    Nice clear post, although I would have preferred to see something along the lines of RSS consumption, or Javascript solutions, as more often than not “static” pages are ones which people think of as have no database interaction at all.

  58. davy:

    September 6th, 2008 at 4:56 am

    I tried this code, and I get the blank page…

    I have the excerpt filled in, but I dont get any text.

    I seen about guid and something else, what are these and how can they be configured to show posts?

    I have been looking for some info on how to show excerpts outside the loop for ages

  59. Terry Connolly:

    September 11th, 2008 at 10:34 pm

    Thank you so much for providing this information, it’s exactly what I’ve been looking for!

    However, do you know how to limit it to certain categories?

  60. Jake:

    September 18th, 2008 at 2:29 am

    WOW…this site is beautiful and you are so knowledgeable! Is it possible to use this technique to call the header and footer instead of the posts?

Leave a Reply

Quicktags: