Title: Pulling Current Category
Last modified: August 19, 2016

---

# Pulling Current Category

 *  Resolved [isserdude](https://wordpress.org/support/users/isserdude/)
 * (@isserdude)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/pulling-current-category/)
 * Hi,
 * I am trying to have my single posts pull the current category (and subcategory
   when appropriate) in the sidebar, but I don’t know what to replace the “8” with
   in the following code. When I include a category number (like 8), it works great,
   but I can’t figure out how to automatically have it pull from the current category.
   Any help would be GREATLY appreciated. I am not a PHP programmer and this is 
   my first time implementing this. Everything else on the site works great, but
   now I’m stuck with this final piece.
 * >  <?php if ($studiobox_sidebarfeatured_style==”Random from Category”) {
   >  $my_query
   > = new WP_Query(‘cat=8&showposts=1&orderby=rand’); } else { $my_query = new 
   > WP_Query(‘cat=8&showposts=1’); } ?>
 * Thanks!!

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/pulling-current-category/#post-1626930)
 * Try:
 *     ```
       <?php
       $category = get_the_category();
       $current_category_id =  $category[0]->cat_ID;
   
       if ($studiobox_sidebarfeatured_style=="Random from Category") {
       $my_query = new WP_Query('cat=' . $current_category_id . '&posts_per_page=1&orderby=rand');
       } else { $my_query = new WP_Query('cat=' . $current_category_id . '&posts_per_page=1'); } ?>
       ```
   
 *  Thread Starter [isserdude](https://wordpress.org/support/users/isserdude/)
 * (@isserdude)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/pulling-current-category/#post-1627101)
 * That did it! Thanks so much!

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

The topic ‘Pulling Current Category’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [isserdude](https://wordpress.org/support/users/isserdude/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/pulling-current-category/#post-1627101)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
