Posted on 27 March, 2012 By 2 Comments

How to List the Last Post from a Specific WordPress Category with an Excerpt

 

For a recent project I wanted to do something a little different with the list of categories in the sidebar. Instead of simply listing each category, I wanted to list an excerpt from the last post in that category.

I wasn’t quite sure how to do this although I did know that I needed an array of the categories I wanted to use. After a little trial and error and asking a friend a question, I came up with the following piece of code.

<?php $cats_to_get = array(‘facebook’, ‘pinterest’, ‘wordpress’); ?>
<?php foreach($cats_to_get as $cat_to_get): ?>
<?php query_posts(‘category_name=’.$cat_to_get.’&posts_per_page=1′); ?>
<strong><?php single_cat_title() ?></strong>
<?php while (have_posts()) : the_post(); ?>

<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”> <?php the_excerpt(); ?>

<?php endwhile; ?>

<?php endforeach; ?>

The code will produce the following:

Facebook

Bye-bye FBML

Pinterest

Tips and Tools for Pinterest

WordPress Tips

Hide the comments link when comments are closed.

  1. After array, list the categories that you want to include. In my example I am using three – facebook, pinterest and wordpress.
  2. &posts_per_page=1 – this says to show the last post in the specified category. If you want to show more than one post, you can increase this number.
  3. I use the excerpt field when writing my posts, which is why my excerpts are customized and are usually only one sentence. Most of this time this will be the first x number of words from the beginning of your post. You could also link the title of the post, if you didn’t want to use the excerpt.
  4. I linked the excerpt to the permalink for the post, so the user can click the excerpt and read the entire post.

Do you think this is useful? Do you think it’s something that you would use?

photo credit: zerojay




Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




2 Responses to “How to List the Last Post from a Specific WordPress Category with an Excerpt”

Leave a Comment
You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

CommentLuv badge

About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Chris Tusa: NovelSnippet.com is cool. It contains hundreds of books (and seems to be rapidly growing). The books are accompanied by ...
    • Dee: I drop a leave a response when I especially enjoy a post on a site or I have something to contribute to the conversation...
    • Kabsiha: Thanks for your great idea to hide the deactivated comment link in word press this would be the very useful post for who...
    • Pat Alexander: Well now these are really great. Makes it perfect for small sites....
    • Tracy G: I really don't know how to do it, glad you posted it here....

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice