• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Nile Flores Media

Helping You Rock Out Your Website Like A Rockstar

  • Home
  • About
  • Blog
  • Portfolio
  • Hire Me
  • Resources
  • Contact
  • Blogging
  • WordPress
  • Social Media
  • SEO
  • Web Design
  • News
  • Podcast
You are here: Home » WordPress » Display Posts From A Category In WordPress

Display Posts From A Category In WordPress

By Nile Flores 10 Comments


Recently I had a run in with coding with a client who had a developer put together a sloppy custom WordPress query in place of a proper loop. In fact, it was amazing because it was put in replace of code on a great premium theme by StudioPress. By the way, no it was not StudioPress who did it, so let me get that out of the way. They are good at what they do. 🙂

Anyhow, the custom query was three different pieces of code: a section to show to first 5 most recent posts, and then two columns underneath to show two different categories that the site owner wanted to feature. The was done using a similar solution to the one At Weblog Tools Collection called Define Your Own WordPress WP-Query.

The problem was that this solution was not providing a good pagination. The “Next” and “Previous” did not work, and even if I wanted to use my favorite WP-PageNavi plugin, then I was pretty much – “sorry, out of luck.” This does not work well as a replacement for the regular loop, especially for what it was being used as.

So, I set out to put together a better solution. In the end, I displayed the most recent posts by normal Loop means. For the categories, I used the following:

<?php $recent = new WP_Query(); ?>
<?php $recent->query('cat=1&showposts=3'); ?>
<?php while($recent->have_posts()) : $recent->the_post(); ?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></h3>
<div class="attachment-post-thumbnail">
<?php the_post_thumbnail(); ?>
</div><?php the_content_limit(130, ""); ?>
<?php endwhile; ?>

We are still using a custom WP_Query, but only using it for the categories. You can replace the 1 after cat=1 to whatever category you want to display, and you can change the number of posts displayed by changing the number in showposts=3.

You can customize the code further with CSS and adding other attributes.

I left some styling to include the thumbnail, title, and a short excerpt, even using the_content. However, you can change it to the_excerpt if you have set your excerpts through your functions.php or excerpt plugin. This code goes in your WordPress theme template where you want it, however I use this normally for the theme’s template file that is for the front page of the website. This is good for sites that are more magazine-like.

Sound easy?


Filed Under: WordPress Tagged With: custom wordpress query, wordpress tricks

About Nile Flores

Nile is a 43-year old female from the greater St. Louis (Southern Illinois side) area. Nile is a mother of 1 son. She is also a web designer and developer, a graphic designer, and a public speaker, who exclusively designs and develops using WordPress. She also blogs at GoDaddy's Blog, Verpex Hosting's blog and her very personal sites, Pixelled and Nail Polish Happy.




Related posts:

No related posts.

Reader Interactions

Comments

  1. Henry says

    January 28, 2012 at 8:18 am

    Can you do something similar for custom post types? I would like to be able to display a list of posts (maybe 5 posts) that are of the CPT “events” but am struggling with the code.

    Reply
  2. Chris says

    January 28, 2012 at 4:34 pm

    I tried this and could not get it to work. I’m probably doing something wrong, but then i found out my theme has a widget that can display posts from a certain category in the sidebar which I’ve since started using instead. I’m sure it’s something with my theme.

    Reply
  3. Katie Woodard says

    January 30, 2012 at 12:37 pm

    It sounds like your client had gotten ripped off. Good thing they had you to turn to. This looks a little complicated. I wouldn’t have been able to help them.

    Reply
  4. Ricardo says

    January 31, 2012 at 7:01 am

    Hi Nile,

    I have come across both “WP Query” and “query_posts” and seen both approaches used for the same thing.

    Can I ask what the difference is? I am trying to get my head around the WordPress loop.

    Thanks!

    Reply
  5. Jonathon says

    February 5, 2012 at 10:17 pm

    Found this interesting although coding is outside my skill set. I had used Flexibility3 free theme on 5 of my sites because I had seen other sites using it that I liked the look of. Being a non-techie I found trying to configure the sites frustrating so on the advice of a more experienced marketer I’m looking at the Studiopress premium themes which I understand are reasonably easy for the technically challenged to set up. Would you agree?

    Reply
  6. Aaron Turner says

    February 19, 2012 at 7:55 am

    Customized wordpress queries are the way to go, people want what they want and how they like it and the more unique things are even better.

    Reply
  7. Mel says

    February 22, 2012 at 3:16 am

    I have been searching for such a long long long time for a wp_query that worked. Thank you so much. You really made my night. Definitely subscribing to you. My site is almost ready but I am still figuring out how to display the different categories on the index page. =/

    Reply
  8. Anonymous says

    February 25, 2012 at 10:28 pm

    hi i am looking for custom_type can you tell me for this i have suscribe your site to look forward thank u

    Reply
    • Nile says

      February 26, 2012 at 1:59 am

      If you have custom post types and have enabled categories, you can put the Loop for the custom post type in the wp query instead.

      Reply
  9. jobin says

    March 14, 2012 at 4:55 am

    Thank you for a post….This looks a little complicated.

    http://www.findghar.com

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow Me On:

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Get The Latest WordPress & Blogging Tips

Sign up for my newsletter, and also get my free ebook on 10 Reasons Why Your Website Isn't Converting, and How to Fix it!




Try to be More Positive on Social Media

Note: You can click on image and view the video in a larger lightbox window.

Footer

The Blog

Lots of free information, tutorials, and more to help you bring your best foot forward with your website.

  • Blogging
  • WordPress
  • Social Media
  • SEO
  • Web Design

Get The Latest WordPress & Blogging Tips

Sign up for my newsletter, and also get my free ebook on 10 Reasons Why Your Website Isn't Converting, and How to Fix it!

Copyright © 2025 · · WordPress