Title: Auto-detect Category
Last modified: August 19, 2016

---

# Auto-detect Category

 *  Resolved [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/)
 * The code I’m using right now is displaying the 6 newest posts, but I’d like to
   have it auto-detect the category the post is in, so in single.php and category.
   php the code would display the posts in that specific category.
 * Can someone help me with this?
 *     ```
       <?php $my_query = new WP_Query('showposts=6');
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID; ?>
   
       <div id="contentslider_menu"><div class="contentslider_menubalk_streep"><div class="contentslider_menubalk"><ul><li><a href="<?php the_permalink(); ?>" class="toc">
   
       <p class="wit"><?php the_title(); ?></p>
       </a></li></ul></div></div></div>
       <?php endwhile; ?>
       ```
   

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/auto-detect-category/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/auto-detect-category/page/2/?output_format=md)

 *  [kapiljainin](https://wordpress.org/support/users/kapiljainin/)
 * (@kapiljainin)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068286)
 * I think you are looking for display related posts. If yes, there are some plugins
   which do that.
 * Just search on google for _display related posts wordpress plugin_
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068287)
 * Isn’t there a way to do this with, excluding category if category = 1 …. or something
   like that.
 * I really don’t like to use to many plugins for these kinda things.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068290)
 * You’ll need a function or something to automate it, a plugin is just a series
   of functions, classes, variables etc…
 * Find a basic one that covers what you want, and strip the code out if you don’t
   want to use it as a plugin..
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068304)
 * Agreeing with kapiljain.in and t31os, but for your educational use:
 * In a category template this returns the current queried category:
 *     ```
       $cat = get_query_var('cat');
       ```
   
 * Returns first category on a post:
 *     ```
       $cats = wp_get_post_categories($post->ID);
       $first_cat = $cats[0];
       ```
   
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068306)
 * [@michaelh](https://wordpress.org/support/users/michaelh/):
 * Thank you for the educational bit, but I have to admit that I know nothing or
   very little about implementing PHP code.
 *     ```
       <?php $my_query = new WP_Query('showposts=6');
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;
       ?>
       ```
   
 * Where in this bit should I try to implement
 * `$cat = get_query_var('cat');`
 * ?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068324)
 * If you are using a [category template](http://codex.wordpress.org/Category_Templates),
   WordPress will handle the query for you.
 * But just in case you need it:
 *     ```
       $cat = get_query_var('cat');
       $args=array(
          'cat__in' => array($cat),
          'showposts'=>6
          );
       $my_query = new WP_Query($args);
       ```
   
 * See [template tag](http://codex.wordpress.org/Template_Tags), [query_posts()](http://codex.wordpress.org/Template_Tags/query_posts)
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068383)
 *     ```
       <?php
       $cat = get_query_var('cat');
       $args=array(
          'cat__in' => array($cat),
          'showposts'=> 6
          );
       $my_query = new WP_Query($args);
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;
       ?>
       <div class="contentslider_menu">
       <div class="contentslider_menubalk_streep">
       <div class="contentslider_menubalk">
       <ul><li>
       <a href="<?php the_permalink(); ?>" class="toc">
       <span class="knop_titel"><?php the_title(); ?></span>
       <span class="knop_datum"><?php the_time('j F Y H:i'); ?></span>
       <span class="knop_category"><?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';} ?></span>
       </a>
       </li></ul>
       </div></div></div>
       <?php endwhile; ?>
       ```
   
 * I’m using this now, but it still gives me the 6 newest posts from all categories.
   I’m using this code in the header.php because I want to minimize my code, I wanted
   it to work on category.php and single.php
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068405)
 * You’ll want that in a category template.
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068504)
 * [@michaelh](https://wordpress.org/support/users/michaelh/):
 * It doesnt work, it still gets me all news posts of all categories combined, but
   that kinda makes sense, it shouldn’t matter if its in the header of single or
   category.php right?
 * I don’t want to make a catergory-1.php category-2.php etc. i’m really trying 
   to minimalize the code.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068509)
 * This is wrong
 *     ```
       $args=array(
          'cat__in' => array($cat),
          'showposts'=> 6
          );
       ```
   
 * change to
 *     ```
       $args=array(
          'cat' => $cat,
          'showposts'=> 6
          );
       ```
   
 * Of course, I don’t know why you are showing posts for the given category in a
   category template–seems repetitive.
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068521)
 * [@michaelh](https://wordpress.org/support/users/michaelh/):
 * It did the trick for categories but with single posts it still shows the 6 newest
   posts of all categories.
 * The reason I want this to work, is because i’m using a content-slider in the 
   header. I want it to show the posts in the specific categories and of specific
   categories. On the homepage it does what it has to do, showing all the newest
   posts of all categories.
 * [link moderated]
 * I’m sorry for not being clear enough ….
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068537)
 * For a single post template, this should work:
 *     ```
       if ( is_single() ) (
       $cats = wp_get_post_categories($post->ID);
       $args=array(
          'cat' => $cats[0],
          'showposts'=> 6
          );
       }
       ```
   
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068544)
 *     ```
       <?php
       if ( is_single() ) (
       $cats = wp_get_post_categories($post->ID);
       $args=array(
          'cat' => $cats[0],
          'showposts'=> 6
          );
       }
       $my_query = new WP_Query($args);
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;
       ?>
       ```
   
 * I’ve got that right now, but this doesn’t work.
    I don’t understand the } closing
   bracket… isn’t this suppose to be a ) ?
 * I’ve also tried to close the ) and replace the last } with a ), but that doesn’t
   work either.
 *     ```
       <?php
       if ( is_single(); ); (
       $cats = wp_get_post_categories($post->ID);
       $args=array(
          'cat' => $cats[0],
          'showposts'=> 6
          );
       );
       $my_query = new WP_Query($args);
       while ($my_query->have_posts()) : $my_query->the_post();
       $do_not_duplicate = $post->ID;
       ?>
       ```
   
 * P.S.: Would you be so kind to remove the URL I mentioned some posts above this
   one?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068546)
 * Sorry, this should be:
 * `if ( is_single() ) {`
 *  Thread Starter [deeltje](https://wordpress.org/support/users/deeltje/)
 * (@deeltje)
 * [17 years ago](https://wordpress.org/support/topic/auto-detect-category/#post-1068557)
 * [@michealh](https://wordpress.org/support/users/michealh/):
 * Perfect, but now it only detects on single.php, on the index.php and category.
   php it doesn’t detect any posts anymore 😉
 * Maybe an -else- somewhere?
 * (Thanks very much for removing the url btw!)

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/auto-detect-category/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/auto-detect-category/page/2/?output_format=md)

The topic ‘Auto-detect Category’ is closed to new replies.

 * 22 replies
 * 4 participants
 * Last reply from: [deeltje](https://wordpress.org/support/users/deeltje/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/auto-detect-category/page/2/#post-1068580)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
