How to Show the Latest WordPress Posts in an External Page

We have recently re-organized the Winwaed.com homepage, and needed a widget to show the titles of the latest blog posts. In common with many WordPress installations, the website consists of static pages and a separate WordPress installation. The widget needed to work in a PHP page external to the WordPress installation, but access the WordPress database.

It turns out that this is quite simple. The widget below is designed to allow quite a bit of customization, but you may wish to modify it further.

Here is the widget script:

This produces a section of HTML wrapped in a <section> tag around a <ol> ordered list tag. Each blog post is then contained within an <li> tag. All elements are formatted with CSS classes.

To use this script in a PHP page, you will first need to include it, and define the CSS classes:

Then to call the widget, insert the following PHP at the required location:

The end result can be seen on the homepage. Each post is listed with a hyperlink to the article, the date of publication, and at most two categories. The categories also links to the category index pages.

The core functionality is in the call to wp_get_recent_posts(). This fetches the basic post information. Further information is queried using each post’s ID, or the category IDs. All of the WordPress API is made available using the include to wp-load.php in the WordPress installation.

The above script could be easily modified to show the latest pages instead of posts. For very busy sites, the result could also be cached to a text file on the web server. If the date of the text file is older than 5 minutes (say), then it could be re-generated.

When you update your blog on a regular basis, there is a need to send email updates to you email subscribers. Luckily there is a wide choice of email marketing software, so you can choose MailChimp or find something better than MailChimp. It depends on your marketing goals.

Leave a Reply