When I originally made my blog back in 2018, I used a custom blogging software that I made myself. Although suitable, I never got around to finishing it thus decided to move to WordPress (“WP”) instead. One of the major parts of moving over to WP would be having a Recent Posts section on my non-WP section of laimmckenzie.com, like below.
Why? Most users visit the homepage of a website before any other part of the site, depending on their purpose of being there. A major part of moving to WP meant that I was putting my blog on a different domain – having it on the same domain under a directory could get messy very quickly, it also looks nicer in my opinion.
Ok cool, now the boring part is over. Here’s how to actually do it. When you create a WP blog it creates a new database on your MySQL Server, with a bunch of tables in it. This is the same as every other Database you’ll ever look at it, it’s just super messy. The way I’m going to show is this is using PDO, but you can do this any way you like really, the process is the same.
Once you have the basic database connection, you’ll need to start connecting your chosen page to that database. Create a file called index.php and copy in the below, this will link the index page up to the connection file and function file that is yet to be created.
The above is a basic idea of the integration required, the conn.php file is the connection to the database, the funcwp.php file is the functions that you will call on the pages to get the actual data out of the database – both of these files are required for every page that you want WP content on.
Now that we have the basic page set up, we need to create the funcwp.php file. In this example, I will be pulling the 5 most recent posts from the database, with their status as published – this means it will not include drafts or posts published as private.
The table that stores all of your posts on WP is called
wpposts. The columns that we will be using are
post_date. A full list of columns for
wpposts can be found here, however the above should do for most situations.
At this point you should have three files – conn.php, funcwp.php and index.php – at the moment none of your data is actually going to be shown, a simple foreach loop will allow you to pull the data from the function and output it how you like, an example of this is below.
That’s pretty much it. Assuming your database connection is valid, and you have some posts in your
wpposts table that match the criteria chosen, if you load up index.php on your server you should be able to see your most recent 5 posts outputted in the page.