Blog Pile

WP LastPosts: Cause I Keep Forgetting My Own URLs

I just whipped up the puniest of WordPress hacks for the ultimate reasons, to serve myself. If I knew what I was doing, it would be a plugin, but I’ll leave that as an exercise for the reader.

Here’s the thing. Whenever I write something, i like to reference by link a previous post. So I have to load my own site to find it, and man, I am tired of waiting. So my hack generates page that just in simple layout lists your latest 100 (or whatever number you wish) posts, so you can either copy the URL or edit it.

It makes something like this:

The code consists of 2 PHP files and a GIF image that go into your theme directory:

  • headless.php is just a simplifed version of header.php with just some CSS in it
  • last.php is a simple template that does all the work
  • images/edit.gif is just an icon for the output

A walk through of the code of last.php follows for the curious… This first part merely provides what WP needs to list it as named template

This is a little bit of useful trickery to make any page that uses this template use a different file for the header, instead of header.php- I use this often when I need to insert custom things in the HEAD of an output page:

Just the HTML set up for lists…

Now we start the work; we first run a custom query to get the last 100 posts (change the number if you want more or less). This iterates your own version of the WordPress Loop. We set up a while loop so we can walk through each one, and we insert what is yanked from the database for a post, all of the information about/in a blog post.

And here we echo what is needed to display info for one post, link to it, its title, date and time of posting, and a link to edit it (only if you are logged in with edit privs):

And the rest just closes things up, nice and neat:

Now to make it active, all you need to do is to create a new WordPress Page. You can put whatever you want in the title or body, cause it is never used on the output. I edit the URL path to be something like:

Now the thing to make it work is to go down to to Page Template, and select the name of this template (I called it Last XX Posts), and then Publish.

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff and blogging Alan Levine barks at on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person. And he is 100% into the Fediverse (or tells himself so) Tooting as


  1. So can you get an RSS version and maybe a json version ) of this out too? ‘cos that could maybe feed into a panel that gets popped up n=by a linkback panel that allows you to link other related pages back to your original post…

    Here’s the use case (“How best to accrete additional content around a post, once it has been published, to grow it as a resource over time”):

Leave a Reply

Your email address will not be published. Required fields are marked *