WordPress – Display the most Recent Post from a Specific Category

Did you see sites with a magazine style theme who are displaying posts from a specific category. Sometimes only the most recent post.

Well you can do this too easily.

&lt;?<a title="See also WordPress - Control When Your Posts are Available via RSS" href="http://www.whileifblog.com/2012/03/19/wordpress-control-when-your-posts-are-available-via-rss/">php</a>
   query_posts('showposts=1&amp;cat=3');
  while(have_posts()) : the_post();
?&gt;
    &lt;ul&gt;
    &lt;li&gt;&lt;h3&gt;&lt;a href=&quot;&lt;?<a title="See also WordPress - How to List Your Upcoming Posts" href="http://www.whileifblog.com/2011/09/08/wordpress-how-to-list-your-upcoming-posts/">php</a> the_permalink() ?&gt;&quot; rel=&quot;bookmark&quot;&gt;&lt;?<a title="See also WordPress - Create Your Own Popular Posts content in the sidebar" href="http://www.whileifblog.com/2011/01/09/wordpress-create-your-own-popular-posts-content-in-the-sidebar/">php</a> the_title(); ?&gt;&lt;/a&gt;&lt;/h3&gt;

    &lt;ul&gt;&lt;li&gt;&lt;?<a title="See also WordPress - Batch Delete Post Revisions" href="http://www.whileifblog.com/2010/09/22/wordpress-batch-delete-post-revisions/">php</a> the_content(); ?&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;/ul&gt;
&lt;?<a title="See also WordPress - Programmatically Creating Posts" href="http://www.whileifblog.com/2010/09/20/wordpress-programmatically-creating-posts/">php</a> endwhile; ?&gt;

Add the above code anywhere you like in the template.
Make sure you change the category ID and you can change the number of posts displayed as well if you want.

Leave a Comment


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

More in Code, Manual/Reference, Tips, Wordpress (133 of 192 articles)


Introduced in WordPress 2.9, the the_post_thumbnail() function is very useful to easily add and display a thumbnail attached to a ...