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:
http://cogdogblog.com/code/lastposts.zip

  • 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:
http://www.mycoolblog.com/last/

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.

The post "WP LastPosts: Cause I Keep Forgetting My Own URLs" was originally emerged from the primordial ooze and first walked on land at CogDogBlog (http://cogdogblog.com/2008/10/wp-lastposts/) on October 25, 2008.

4 Comments

Leave a Comment

All fields are required. Your email address will not be published.