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.phpis just a simplifed version of header.php with just some CSS in it
last.phpis a simple template that does all the work
images/edit.gifis 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
<?php /* Template Name: Last XX Posts */ ?>
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:
<?php include (TEMPLATEPATH . '/headless.php'); ?>
Just the HTML set up for lists…
<div id="content"> <h1>Last Posts!</h1> <ol>
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.
<?php $my_query = new WP_Query('showposts=100');?> <?php while ($my_query->have_posts()) : $my_query->the_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):
<li><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>""><?php the_title(); ?></a> <small><?php the_time('M j, Y') ?> <?php the_time('h:ia') ?> <?php edit_post_link(' Edit', '<img src="' . get_bloginfo('template_directory') . '/images/edit.gif" alt="" align="absmiddle" /> ', ''); ?> </small> </li>
And the rest just closes things up, nice and neat:
<?php endwhile; ?> </ol> </div> </body> </html>
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.
WP LastPosts: Cause I Keep Forgetting My Own URLs by CogDogBlog, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.