Title: Displaying Category Posts
Last modified: August 19, 2016

---

# Displaying Category Posts

 *  [subtype](https://wordpress.org/support/users/subtype/)
 * (@subtype)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/)
 * I know this is simple, but when i am looking at single.php and an individual 
   post, i want to display the most recent 5 posts in that category. But it obviously
   has to be dynamic so it knows which category your in, how do i do this?
 * I have this and it doesn’t work; it displays posts from all categories:
 * $cat = get_query_var(‘cat’);
    global $post; $myposts = get_posts(‘numberposts
   =5&offset=1&category=$cat’);

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [subtype](https://wordpress.org/support/users/subtype/)
 * (@subtype)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/#post-693194)
 * anyone? anyone at all?
 *  [SimonJ](https://wordpress.org/support/users/simonj/)
 * (@simonj)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/#post-693231)
 * Yo!
 * Read this thread :
 * [http://wordpress.org/support/topic/155342?replies=12](http://wordpress.org/support/topic/155342?replies=12)
 * S.
 *  Thread Starter [subtype](https://wordpress.org/support/users/subtype/)
 * (@subtype)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/#post-693279)
 * That doesn’t work – I tried get_the_category(); with no success.
 *  [SimonJ](https://wordpress.org/support/users/simonj/)
 * (@simonj)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/#post-693311)
 * Well… You can’t just use only the `get_the_category();` tag, alone, as is…
 * You have to build a loop with the result :
 *     ```
       <?php $cat = get_the_category(); $cat = $cat[0]; ?>
       <?php
       query_posts(array(
       'cat'=> $cat->cat_ID,
       'showposts'=>'5',
       ) );
       ?>
       <?php while (have_posts()) : the_post(); ?>
       --- DO YOUR STUFF (list titles, authors, date, etc ---
       <?php endwhile; ?>
       ```
   
 * So yes, it works… 😉
 * S.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Displaying Category Posts’ is closed to new replies.

## Tags

 * [list](https://wordpress.org/support/topic-tag/list/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [SimonJ](https://wordpress.org/support/users/simonj/)
 * Last activity: [18 years, 4 months ago](https://wordpress.org/support/topic/displaying-category-posts/#post-693311)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
