Title: &quot;in_category()&quot; function help
Last modified: August 21, 2016

---

# "in_category()" function help

 *  Resolved [Debugger](https://wordpress.org/support/users/retrobeatcom/)
 * (@retrobeatcom)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/)
 * Hello,
    first of all i want to Thank You in advance for the help!
 * And here’s my question:
 * On the index page i want to place a small icon next to the post name for posts
   that are in specific category. Lets say that i have this categories structure:
 *     ```
       Main Cat1
       --Sub Cat1
       --Sub Cat2
       --Sub Cat3
       Main Cat2
       Main Cat3
       etc.
       ```
   
 * So i need all posts that are in sub-catgories 1,2 and 3 to have this icon.
    The
   simple solution:
 *     ```
       <?php if ( in_category( array(Sub Cat1 ID,Sub Cat2 ID,Sub Cat3 ID)) {echo "<img src='ICON URL'w>";} ) ?>
       ```
   
 * But in this case i should add every new sub-catgory manually.
 * I tried this:
 *     ```
       <?php if ( in_category(Main_Cat1 ID) {echo "<img src='ICON URL'w>";} ) ?>
       ```
   
 * But its not working.
 * If anyone can suggest a solution for this?
    Thanks!

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

 *  [ubaidullahbutt](https://wordpress.org/support/users/ubaidullahbutt/)
 * (@ubaidullahbutt)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685169)
 * Don’t use in_category(), instead use is_category(), the function will absolutely
   work.
 * Read full doc here [http://codex.wordpress.org/Function_Reference/is_category](http://codex.wordpress.org/Function_Reference/is_category).
 *  Thread Starter [Debugger](https://wordpress.org/support/users/retrobeatcom/)
 * (@retrobeatcom)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685246)
 * Hi, yes i previously tried with is_category() function but with no success. I
   just tried again and it is not working.
    Any ideas?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685247)
 * You need to use [in_category()](http://codex.wordpress.org/Template_Tags/in_category).
 *  Thread Starter [Debugger](https://wordpress.org/support/users/retrobeatcom/)
 * (@retrobeatcom)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685248)
 * yes – im using it nut it is not working in this case:
 *     ```
       CATEGORY ID1
       -SUB-CATEGORY ID2
       --POST 
   
       <?php if ( in_category( '1' ) {echo "<img src='ICON URL'w>";} ) ?>
       ```
   
 *  Thread Starter [Debugger](https://wordpress.org/support/users/retrobeatcom/)
 * (@retrobeatcom)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685258)
 * FOUND IT!
 *     ```
       <?php
       /**
        * Tests if any of a post's assigned categories are descendants of target categories
        *
        * @param int|array $cats The target categories. Integer ID or array of integer IDs
        * @param int|object $_post The post. Omit to test the current post in the Loop or main query
        * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
        * @see get_term_by() You can get a category by name or slug, then pass ID to this function
        * @uses get_term_children() Passes $cats
        * @uses in_category() Passes $_post (can be empty)
        * @version 2.7
        * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
        */
       if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
       	function post_is_in_descendant_category( $cats, $_post = null ) {
       		foreach ( (array) $cats as $cat ) {
       			// get_term_children() accepts integer ID only
       			$descendants = get_term_children( (int) $cat, 'category' );
       			if ( $descendants && in_category( $descendants, $_post ) )
       				return true;
       		}
       		return false;
       	}
       }
       ?>
       ```
   
 * The above code goes into the function file of the template.
 * and bellow is the solution:
 *     ```
       <?php if ( in_category( 'maincat' ) || post_is_in_descendant_category( 1 ) ) { echo "<img src='IMG URL'>";}?>
       ```
   
 * Thanks **esmi**!

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

The topic ‘"in_category()" function help’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [Debugger](https://wordpress.org/support/users/retrobeatcom/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/in_category-function-help/#post-3685258)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
