How to display recent posts outside WordPress

As fantastic as WordPress is, it can often be installed alongside an existing static website, rather then be used exclusively as a root CMS. The great news is that with a little PHP, you can access your WordPress installation’s Loop and fetch your posts.

Before you use the PHP code, make sure your page or template file is saved with the .php extension rather than .html etc. Assuming your WordPress installation is in a sub-directory called ‘WordPress’, copy and paste the following code onto your page:

If the WordPress folder has been renamed and/or at a different path, simply adjust the path to wp-load on line 3. You can also adjust how many posts are brought out on line 4, by changing the number next to ‘showposts’.

  • James Kemp

    Looks good, but I’d consider using wp_query over query_posts, as described here:

    • Thanks James, I’ll give that a read just now and get the Gist updated.

  • Mar

    Thanks for the clean code that work!

  • Tim Ottewell

    Can this be done on a site which is on a different server to the WP instance?

    • daniel_c_long

      Hi Tim, unfortunately not. You won’t be able to include your WordPress’s wp-load.php file if it’s cross server. You can’t load the file using http://, it has to be done locally I’m afraid.

      • Tim Ottewell

        So would I be right in thinking you’d have to utilise the RSS feed if you wanted to create a WordPress “widget” for a separate site?

        • daniel_c_long

          Yes that’s right. You’d need to aggregate it using an RSS Feed for example.

        • Dan is spot on Tim, utilising the RSS feed would be the only way. There are some old-school tools that allow you to do this like Magpie RSS & Simplepie, although they’re a little bulky.

  • kitjai

    Thanks for this!

    I’m wondering why the post doesn’t truncate based on where you’ve put the ‘MORE’ comment on the post itself as it would have for the actual blog page?

  • Organicbabyhut

    Can i change the size of the thumbnail ?

  • Jolyon Favreau

    smashing piece of code…though i have altered it slightly to be bootstrap friendly.

    how might one reference several categories…, 5, 12, 96…would that be possible?

    many thanks

    • Hi Jolyon, yes that would be possible, line 5 (of my code above) would just need uncommenting and adjusting to include multiple category IDs. Would be interesting to hear how you’ve altered the code to make Bootstrap friendly.

      • Jolyon Favreau

        i’ve tried adding specific categories but it doesnt like my method…….. do i use an operator? ie ==, and, or etc

        • Hey Jolyon, sorry for the delay in getting back to you, I hadn’t realised you’d replied. try:

          ‘cat’ => array(312,13),

  • libralion

    Thanks for the code. Works great.
    But I want to get rid of the thumbnails. I only want to display the title of the last 5 last posts with a few lines of text.
    What code should I use then? I already tried to cut some lines out, but I am afraid I am not good at this.

    • Hi Libralion, Great to hear that’s working for you. Simply remove lines 14 > 16 and this will take out the thumbnail image. If you also want to remove the date, you can remove line 19 too.

      • libralion

        Thanks James! Works great!
        I am struggling with the css. I don’t want to display the social media icons. I tried adding several classes to my css file, but still no go. Have you any idea what to add to my css file to remove the social media icons below every post?
        This is my testpage:

        • You appear to be running a plugin that deploys the Social Sharing buttons with the excerpt. You’d either need to disable this plugin, or you could hide it with CSS if you prefer: You’d need something like the following:

          #footerArea2 .addtoany_share_save_container { display: none !important; }

  • CinemaLive.TV

    Eh, not working for me .. “Error establishing a database connection” 🙁

    • Hi there, sorry to hear it’s not working for you, do you have WordPress installed in a /blog or /wordpress directory or is it on the root?

      • CinemaLive.TV

        Hi, thanks for reply. I have my own site in root folder (in standart cPanel folder public_html) and in the same folder wordpress (public_html/blog). I tried other scripts who display recents posts and they works. … but your script looks much more better, only it does not work for me .. every time the same “Error establishing a database connection” 🙁
        Maybe this script does not work on latest wordpress 3.9.2?

        • Thanks for the update, great to hear you got it working ok.

      • Pallab Baruah

        Hi James, I am having the same problem. My blog is installed in /blog directory and I have pasted your coded in /views/home.php. I am getting “Error establishing a database connection”. Please help.

  • Thanks a lot, what a great script! I was using domdocument to load rss feeds and display it but that stopped working for some bizarre reason so this was the perfect alternative!

    • Thanks for the feedback Andy, glad to hear that’s working great for you.

  • Adrian Wu

    hi, thx , at the beginning I want to use the WP-REST API to do that. But yours is much more convenient and handy . So I decided to use yours and it works well except the CSS issue. All the CSS lost when compares to the original WordPress site. Could you please tell me how to recover the CSS ? thanks again.

    • Hi Adrian, pulling your latest posts onto a PHP outside of your WordPress installation won’t bring out any of the stylesheets from your WordPress theme. You’d either need to add your stylesheet to your static PHP site, or copy the desired CSS to ensure they styling is maintained as per your WordPress site.

      • Guest

        hi, thx ,for the reply. can i just return the post img url instead of because it will be easier for me to styling it.?


        • You certainly can, just simply bring that back on it’s own by removing items you don’t want.

          • Adrian Wu

            i want to return the post img url instead of the_post_thumbnail() which includes the url but can not be edited.

      • Guest

        hi, thx ,for the reply. can i just return the post img url instead of because it will be easier for me to styling it.?


      • Adrian Wu

        hi, thx ,for the reply. can i just return the post img url instead of the_post_thumbnail() because it will be easier for me to styling it.?


  • Adrian Wu

    hi, James, your ?php the_excerpt(); ? only extract the text without html tag in the WP page text editor area . So there is no way to add the the CSS after php returning the HTML page. Please tell me how to solve it, thx

    • Hi Adrian, that’s right the_excerpt() funtion only returns standard text, whether it’s auto generated or specified by you. If you wanted to return HTML content, you could bring out the_content() which would return the whole post content?

  • JP15

    Hi James, Thanks for this. I believe what you describe above is exactly what I am trying to do. I have a wordpress blog and a static site (not wordpress – different host) that I want to pull the blogs on to. I am having issues with the require path to wp-load as it is not on the document root of the static site. How can I link to this on a wordpress setup from with a static non wordpress site, or is it not possible?

    • Hi, if you’re trying to connect to a WordPress site on a different server/domain then unfortunately this script wouldn’t work. This script will only connect to WordPress if the static site and WordPress installation are within the same site. Hope that answers your question.

  • Moya

    thank you for this great tutorial it works great however i keep getting this error message, please help..Warning: Cannot modify header information – headers already sent by (output started at /home4/………index.php:490) in/home4/………wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 62

  • Bhupinder Singh

    Hi james my page is redirecting to my blog….? please help

  • Luke McLachlan

    thank you!

  • Yudi Catur

    Is it works? How to put my wp URL on this script?