Title: listing category posts dynamically
Last modified: August 19, 2016

---

# listing category posts dynamically

 *  [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/listing-category-posts-dynamically/)
 * Hi
 * In my sidebar I’m attempting to list the posts from the current category. ie.
 * If in a specified parent category, dynamically get the child category then list
   it’s posts or if in other parent specified category display something else.
 * I feel like I’m close but my php isn’t great and I’ve got issues with it displaying
   differently from single.php to category.php
 * Please someone point me in the right direction:
 * Code now in paste bin:
 * [http://wordpress.pastebin.com/tnmVZX0j](http://wordpress.pastebin.com/tnmVZX0j)

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

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/listing-category-posts-dynamically/#post-1878783)
 * in single.php, `get_query_var('cat')` is not defined;
 * to get the categories of a single post, try to use:
 * `get_the_category( $id )`
 * [http://codex.wordpress.org/Function_Reference/get_the_category](http://codex.wordpress.org/Function_Reference/get_the_category)
 * if you restrict the categories of a single post to one category, you should be
   able to get this one with some code like this:
 *     ```
       <?php
       if( is_single( ) ) {
       $category = get_the_category();
       $yourcat = $category[0]->slug;
       }
       ?>
       ```
   
 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/listing-category-posts-dynamically/#post-1878840)
 * Hi Alchymyth
 * Thanks for your help. Unfortunately I seem to be going backwards and getting 
   confused. I couldn’t seem to make it work and so have found other code which 
   at it’s core seems to do a better job but I need to set up a conditional tag 
   to execute the code – or not – according to it’s parent category.
 * The basic code i got from here:
 * [http://wordpress.org/support/topic/how-do-i-display-post-titles-from-current-category-and-highlight-active-post](http://wordpress.org/support/topic/how-do-i-display-post-titles-from-current-category-and-highlight-active-post)
 * but they never posted the final code to stop duplication of the posts on the 
   category page and my conditional code seems to send it in an infite loop.
 * Anyway here’s my code if someone could take a look i’d hugely appreciate it:
 * [http://wordpress.pastebin.com/0jM8SDEY](http://wordpress.pastebin.com/0jM8SDEY)

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

The topic ‘listing category posts dynamically’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [formica](https://wordpress.org/support/users/formica/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/listing-category-posts-dynamically/#post-1878840)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
