Tip#1: Recent Updates in WordPress Sidebar

*Note: since I no longer have my Mondays occupied with publishing the newest trailer (I instead publish them whenever I find them and add them as an update, rather than a new post), I’ve decided to use Mondays to share some tips I’ve discovered while working with WordPress.


When I first was looking to update the site to it’s current snazzy look, I definitely had a few changes in mind. The first and foremost was to provide some sort of “Updated” section to let visitors know what had been recently changed/added, etc. Thanks to the new tabs in the sidebar, I had a spot for these updates…now the only question was how to get the changes to show.

I had heard about “Asides” before, but wasn’t really sure what they were. Doing a bit of research, I discovered they are basically just small snippets of info. This seemed perfectly suited to my needs…at first. Per info I garnered over on wordpress.org, I created an extra category - named “Asides” naturally - and begin adding my updates.

As I started doing that, however, I realized this wasn’t really what I was looking for, for a couple of reasons.

Problem 1 : There’s Nothing To See Here

I didn’t want these tiny updates available anywhere but in my update section, and these Asides were now being displayed in their own category! I set about fixing this, through a complicated process of excluding the category number from everywhere categories and posts displayed. This turned out to be quite numerous, and included the category listing in the “Sections” tab, the previous and next post links at the bottom of each single page, the search page, the singe page template, the category template and the home page, to name a few. While this was time-consuming, I figured this was the answer to my problem.

Problem 2: What’s My Name?

Then I ran into a really silly problem…at least it seemed that way to me. What the heck was I going to name each of my new “Asides”? I started out trying to name them by day (122107_1, 122107_2) but that got annoying, since I would have to go back and try to figure out where I left off every time I had to add a new Aside. I figured this was just going to be something I’d have to live with, however, and kept plugging away.

Problem 3: I’m Lazy

And the straw that “broke the camel’s back”, so to speak? It was the extra step. Whenever I updated something on the site (ie. added a thumbnail to a post, corrected a spelling mistake etc.) - something I was doing a lot during the site redesign - I had to create a new post stating every little thing I did. While this seems minor, it didn’t seem so minor to me after even my 30th update of the day. Plus, add to that I had to find the web address of each post to reference it, and these Asides were getting to be too much work.

By this point, I realized this wasn’t working for me, and had to find a better solution. I needed something I could show in the sidebar; something that would automagically include the link to the post (and eliminate any of those “fat-fingered” typing errors I tend to have); something I wouldn’t need to create a new post to add (thus eliminating the naming issues).

Saved By Custom Fields

As I mulled it over in my mind, I came up with a perfect solution: Custom Fields!

For those of you who don’t know what those are, they are located on the Write Post page of WordPress - way at the bottom - and are incredibly useful ways to add information to posts. With the help of an extremely useful plugin called the Get Custom Field Values by Scott Reilly, they are incredibly easy to display as well.

With that plan in mind, I went to my most recently updated post, added a Custom Field called “updated” and wrote what I updated - simply “Added thumbnail image to”.

Then I went into my sidebar template, and added the following block of code:

Code

<?php rewind_posts(); ?>
<ul>
<?php $r_query = new WP_Query('showposts=5&orderby=modified');
while ($r_query->have_posts()) : $r_query->the_post(); ?>
<li><?php echo c2c_get_custom('updated', '', ''); ?><br />
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>

Code Explained

Ok let’s break it down…
<?php rewind_posts(); ?> - Tells WordPress to search through all the posts. If this is left out, WordPress will start with the post showing and go from there.

<ul> - Starting a separate section for the “Asides” - if you want to style this with CSS, simply change this to <ul class="something"> and then add CSS stylings to ul.something

<?php $r_query = new WP_Query('showposts=5&orderby=modified'); - This tells WordPress to go through the posts and show the last 5 that have been modified. If you want to change how many are displayed, simply change the 5 to whatever number you like.

while ($r_query->have_posts()) : $r_query->the_post(); ?> - This tells WordPress that the following lines show how to display each of the posts it’s found.

<li><?php echo c2c_get_custom('updated', '', ''); ?><br /> - This is how we’re displaying the post. The <li></li> tells WordPress this is a list-item. The code following that is from the Get Custom Field Values plugin and shows that we want to display the “updated” custom field as is. This code is follow by a line break, so the link to the post will be shown on a separate line.

Note: Of course, by removing this line, you remove the need for the “updated” custom field on posts, but just showing the post title seemed a little too vague to me. After all, wouldn’t visitors want to know what exactly about the post had been updated, rather than trying to figure it out for themselves? I thought so.

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> - This is the actual link to the post, followed by the ending of the list-item.

<?php endwhile; ?> - This tells WordPress we’re done showing how to display each post, and </ul> closes this particular section.

That’s The Ticket!

Presto! With that in the sidebar, our Updates eliminated the need for an extra category (thus eliminating the need to hide the category), eliminated the need for extra posts, and handily also eliminated typos in the post links! Now when I update something, all I need to do is add the “updated” custom field to that post with whatever I updated (always remembering the post title will display after it) and voila:

Added thumbnail image to Tip#1: Recent Updates in WordPress Sidebar

Looking back, I realize I was making this harder on myself by trying to use the popular Asides for a purpose other than what they are meant for (short comments on random subjects). Using this “Recent Updates” code instead, I have exactly what I was looking for - a very easy way of displaying the most recent updates in the sidebar.

NOTE: For more information about custom fields, be sure to read what the WP Codex says about them.

Tagged With

2 Responses to “Tip#1: Recent Updates in WordPress Sidebar”

  1. User Gravatar

    Hi, Thanks for that.What I’m tying to acomplish is almost the same, but I’would like to show only the last 5 posts for just one category.

    Is it possible?

  2. User Gravatar

    Anselmo, If (for example) the category you want to show has an ID of 1, change line 4 to ('showposts=5&orderby=modified&cat=1')

    That should do the trick.

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>