Show latest posts in a plain HTML website custom widget

The question:

I have a plain HTML website with WP in a subfolder (/blog). I need to display latest posts from /blog into the plain HTML webpage.

Is there a PHP script or some kind of widget to achieve this?

Thanks!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Perhaps you could consider using WP REST API to get the posts with a XMLHttpRequest in javascript instead of trying to get php work on .html file(s).

From the REST handbook,

The WordPress REST API provides an interface for applications to
interact with your WordPress site by sending and receiving data as
JSON (JavaScript Object Notation) objects.

And

It provides data access to the content of your site, and implements
the same authentication restrictions — content that is public on your
site is generally publicly accessible via the REST API, while private
content, password-protected content, internal users, custom post
types, and metadata is only available with authentication or if you
specifically set it to be so.

Method 2

1) Run php code in html files

You can’t run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:

AddType application/x-httpd-php .htm .html

This will tell Apache to process files with a .htm or .html file extension as PHP files.
(related answer)

2) Use WordPress functionality outside of wordpress folder

If you able to use php in your html files, you need to include wordpress wp-load.php file to load wordpress functionality. I’m not familiar with your folders’ structure, so you will need to modify a path to this file by yourself.

require_once( '../blog/wp-load.php' );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment