How to display latest posts from a single category in WordPress

How to display latest posts from a specific category without a plugin in WordPress

I’ve just made an addition to my side bar that displays all of the latest items added to my SEO jobs category. It’s a few, simple lines of code and doesn’t nessecary warrant an entire plugin! Here’s how to do it – just insert the code below, and change the category=1 argument to the category number of your choice:

<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

The problem I’m trying to solve is why this job is not displaying on the list even though it’s in the same category as all the others.

I’m off to play with Ubuntu Intrepid Ibex now. expect a report, soon!



Stay Updated: Sign Up for Webinar & New Blog Alerts

14 thoughts on “How to display latest posts from a single category in WordPress

  1. Wiep says:

    The &offset=1 prevents the most recent post (or job) to show up, so removing that part should do the trick.

  2. Hi Wiep! Thankyou – tat worked a charm. See you at SMX?

  3. Andy Bird says:

    thanks for that

    worked a treat when &offset=1 is dropped

  4. arjay says:

    I have worked on sites before using the loop and coding this action myself, but your code is terrific and so easy.

    I am building a widgetized sidebar for a customer using your code in the text-widgets, press releases, updates and more. Thanks so much!

  5. gr8 help thanks for solutions…

  6. wparena says:

    Without plugin to pull content is good idea . Thank for article. Now I’m trying to write on it and apply my blog system.

  7. Ron says:

    I am trying to do a similar thing but not in the sidebar. I would like to display one category when a user initially logs onto the site and whenever the user clicks on the blog menu tab. I also have a list of categories in the sidebar that a user can click to retrieve any of the other categories.
    Can you suggest code or a plugin that would work for this?

    thanks

  8. Daniel says:

    Works like a charm, even for someone who is no php expert. Is there anyway to add sorting functionality on the posts that it pulls through?

  9. Ron says:

    Figured this out for anyone who is interested.

    Just add the following line

    before your loop starts (line below) in index.php in your theme. Be sure to change catname to the actually category number you would like to display.

  10. Ron says:

    the line to add is
    php query_posts($query_string . ‘&cat=catname’);

    before

    php if (have_posts()) :

    be sure to add the before the above statement and change the catname to the category for display.

  11. Daniel says:

    Another question. How can i use custom fields to insert the category={insert category number}? I want to be able to use custom fields to determine which category posts to display on the page.

  12. Addy R says:

    Great code!
    Effortless.

  13. Phipan says:

    Thanks, just what i needed

Comments are closed.