Use jQuery to Jazz Up Your List of Latest Posts – Part 1
A lot of blogs display a list of their most recent or popular posts in their sidebar, more often than not just showing the title of each one. But how can you give your reader an idea of what that post is about by using the excerpt, without taking up too much room? JQuery is our friend!
Originally, this post was going to be a few quick tips on using jQuery in different ways, but this one idea has ended up taking up a lot of room, so this will now be part one of two!
The Idea
Some of my favourite sites display a list of posts in their sidebar, be it most popular as on Pro Blog Design, on a certain topic like Justin Tadlock does with his list of tutorials, or just the most recent as on a huge number of other sites. While they all have juicy titles and great design to pull you in, one of the aims of the excerpt is to make the reader want to read the post even more!
The majority of the time, a blog will display their latest posts on their homepage using the excerpt to give a lead paragraph for what the post is about. So it therefore makes sense that wherever else these posts are displayed, an excerpt would be useful. However, if we simply add the excerpt onto all of these titles, our lists become a little bit too long, and uninviting. While this may be a personal view, I find sites like the ProBlogger homepage too crowded for all the fantastic information that is trying to be portrayed. So how can we find a middle ground? JQuery!
Please note: The following tutorial will only work if you are not using widgets in your sidebar/other widgetised area since it requires using the WordPress loop.
The Aims
There are two main aims of the end product of this tutorial.
- Provide the user with as much information as possible so that their clicks are not wasted. A bad experience on a site can keep a user away for ever. At the same time, this means we are showing off the best of our content, with nothing being sacrificed due to space, which leads to…
- Not taking up yards of space displaying all of our excerpts, which in a sidebar or similar area, can lead to a cluttered look.
The WordPress Code
To output our list of posts in the sidebar, we need to access the WordPress loop. It is possible that you already have something that looks a little bit like the following:
1 2 3 4 5 6 7 8 | <ul class="posts"> <?php query_posts('showposts=10'); ?> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?> </a></li> <?php endwhile ; ?> </u> |
- Note that the class of my unordered list is ‘posts’. This is important for the jQuery later, in which you’ll have to change a couple of things to match your own html
- This first of all says that we only want to show the 5 most recent posts
- We then start the loop
- For each post, we output a list element
<li>, which contains… - A link to the post using
the_permalinkand adding other information withthe_title - We end our list element for each post, and finally…
- End our loop
We now want to add in our excerpt to this. We’ll need to write our own function to do this, because we need to add a class to the excerpt, and the_excerpt automatically outputs <p> tags. We need to add this to functions.php:
1 2 3 4 | function excerpt_class() { $excerpt_class = strip_tags(get_the_excerpt(), '<a><img>'); echo "<p class=\"hidden\">" . $excerpt_class . "</p>"; } |
- This creates our new function,
excerpt_class - defines the variable
$excerpt_classasget_the_excerpt, which returns a string, from which we can get rid of the<p>tags - The second parameter of strip_tags means do not strip out any links or images
- We then echo out a paragraph with our new class of
hidden, and include our variable
We now need to add this right before the end of our list element:
1 2 3 4 5 6 7 8 | <?php query_posts('showposts=10'); ?> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?> </a> <?php excerpt_class(); ?> </li> <?php endwhile ; ?> |
For each post, we should now have an output a little bit like this:
1 2 3 4 5 | <li><a rel="bookmark" title="My Post's Title" href="http://mysite.com/new-post/"> My Post's Title</a> <p class="hidden">The excerpt from my latest post</p> </li> |
Some CSS
The next step is to add the following line of CSS to your style.css file. This hides the paragraph when the page is output. This is so that if a user has JavaScript disabled, they won’t see all of the excerpts, which would go against our aims.
1 2 3 | .hidden { display: none; } |
If you already have this line in your style.css file (have a check to make sure), then you don’t need to add it again.
On the next page, we look at the jQuery code.












9 Comments - Add Yours!
That was a great post Alex! Using Jquery to show the excerpts without wasting space is a really good idea and I love how it’s used on your site.
Might have to implement this on Pro Blog Design as well soon!
@Martin – Thanks a lot! I’m glad you enjoyed it. Make sure you check back next week for the next part in the tutorial. I’ll be looking at working with category lists and a few other jQuery quick tips.
I use Latest Posts in my sidebar but dont want the entire sidebar to become longer when hovering a title.
Is there a way to let the excerpt appear in a bubble (“tooltup”) rather then a paragraph below the title (causing the list to become biggger, temporarily)
Thanks in advance,
@Cmet – Absolutely, and you’ve preempted the next post on the topic! As I said in the last comment, I’m going to be looking at category lists, and I’ll show you how to use a tooltip in that post! Subscribe to my RSS to see when its available.
Hey, first of all, thanks for the straight forward tutorial — its very useful.
I’ve successfully implemented this bad boy in a site, but it is being just a tidbit buggy.
Im using the clicked version of it, but it seems to only work on the first item on the list, and not the rest. do you have any idea why something like this would happen?
@Kristian – Firstly, sorry for the delay in replying. I’m glad you enjoyed the tutorial, my aim is always to explain everything, and not to assume too much. As for the problem you’re getting, I can only think that it must be to do with the jQuery selector and finding its parent. If getting to the parent doesn’t go high enough, then the jQuery won’t find all the elements, but just the one where you already were…if that makes any sense…?! Is it possible to see the code in action, maybe I could sort it that way? Comment back if you need any more help.
Excellent tutorial.I want to use it on my blog.
@Neel – Glad you like it, the next part comes out on Monday, so be sure to check it out!
yeah, since then, i downloaded a little ebook on jQuery. it was totally a selector problem. it works fine now.
however, i was trying to pull something a little trickier, and i was hoping you could help me with it.
in your code, you are dynamically changing the text back and forth on open and close of the tab. I have been trying to make it toggle an image instead, like img1 on open img2 on close, etc.
have any recommendations?
7 Trackbacks