Show Posts By Category

Screen shot 2011-06-04 at 08.53.42

I was working on a website with a news section that I was using as a blog. This news section had various different pages that represented a different WordPress category, therefore, on that page I only wanted to list the posts from that particular category. This seemed simple enough to me, but I couldn’t figure out how to do it!

So I turned to my lovely friends on Twitter and asked them the question. Sure enough, it was very simple to do, and is probably something that I should’ve known how to do. Anyway, here is the code.

<?php query_posts(‘cat=1&showposts=3′); ?>
<?php while (have_posts()) : the_post(); ?>
<article>
<h3><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h3>
<p><?php the_time(‘F j, Y’) ?></p>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
</section>

I hope you guys find this as helpful as I did, unless you already knew how to do it! If you know of a better way then please do comment below.

4,469 views
Co-founder and Creative Director of Creative Intent Ltd, an iPhone app development, ecommerce and web design agency based on the sunny shores of Southwold in Suffolk.

Got something to say? Go for it!