Title: Exclude category
Last modified: August 20, 2016

---

# Exclude category

 *  [studiosx](https://wordpress.org/support/users/studiosx/)
 * (@studiosx)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-10/)
 * I use this code to display 5 recent posts of same category on single post pages
 *     ```
       <?php
       if (is_single( )) {
       	$post_ID = $wp_query->posts[0]->ID;
       	$all_cats_of_post = get_the_category($post_ID);
       	for($i = 0; $i < sizeof($all_cats_of_post); $i++) { ?>
           <div class="recent-cat">
       			<h3>Recently in <?php echo $all_cats_of_post[$i]->cat_name; ?>:</h3>
       				<ul>
       					<?php global $post; $cat_posts = get_posts('numberposts=5&category='.$all_cats_of_post[$i]->cat_ID); foreach($cat_posts as $post) : ?>
   
                           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
   
                           <?php endforeach; ?>
                       </ul>
            </div>  
   
       <?php } ?>
       ```
   
 * My question is: if I have a post under 2 categories or category/subcategory, 
   how can I exclude a category or a subcategory?

The topic ‘Exclude category’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [exclude](https://wordpress.org/support/topic-tag/exclude/)

 * 0 replies
 * 1 participant
 * Last reply from: [studiosx](https://wordpress.org/support/users/studiosx/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/exclude-category-10/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
