Title: Conditional for parent category
Last modified: August 20, 2016

---

# Conditional for parent category

 *  Resolved [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/)
 * I have a parent category:
 * News
 * And under that categories I have sub categories
 * Sports, Featured, & Breaking
 * I’m using `is_category('news');` to check whether the current page is the News,
   however if the user lands on one of the posts from the children categories sports,
   featured & breaking, I’d like to check to see if the parent is News with an if
   statement.
 * I have looked through the Codex and have not been able to find the code to accomplish
   this. Any suggestions?
 * Thanks!

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130740)
 * The obvious solution would be to use:
 *     ```
       <?php if ( in_category( array( 'Sports', 'Featured', 'Breaking' ) )):?>
       [do stuff]
       <?php endif;?>
       ```
   
 * [http://codex.wordpress.org/Template_Tags/in_category](http://codex.wordpress.org/Template_Tags/in_category)
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130744)
 * esmi,
 * Is there a dynamic solution? This list of sub categories is going to continue
   to grow and I’d like to set it up so it’s a dynamic reference to the parent (
   news) category.
 * -foo
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130748)
 * You could write a conditional based on [get_ancestors](http://codex.wordpress.org/Function_Reference/get_ancestors).
 *     ```
       <?php
       // Assumes the id of the News category is 20
       $in_news = 0 // set a flag varable to false
       $cats = get_the_category();
       foreach ( $cats as $cat ) {
       	if( in_array( '20', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true
       }
       if( $in_news == 1) :
       [ do stuff ]
       else :
       [ do other stuff ]
       endif;?>
       ```
   
 * [http://codex.wordpress.org/Function_Reference/get_the_category](http://codex.wordpress.org/Function_Reference/get_the_category)
   
   [http://codex.wordpress.org/Function_Reference/get_ancestors](http://codex.wordpress.org/Function_Reference/get_ancestors)
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130944)
 * esmi,
 * This is the code I’m using, however it’s not working correctly:
 *     ```
       <h1 id="logo">
       <?php
       $in_news = 0; // set a flag varable to false
       $cats = get_the_category();
       foreach ( $cats as $cat ) {
       	if( in_array( '23', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true
       }
       if( $in_news == 1 || is_category(23)) : ?>
       <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo-old.png"></a>
       <?php else : ?>
       <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo.png"></a>
       <?php endif; ?>
       </h1>
       ```
   
 * The else works on all pages. ‘is_category(23)’ works for the news page. However
   for all news sub categories, it’s showing logo.png instead of ‘logo-old.png’
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130954)
 * are you using this in a category archive psge?
 * the `get_the_category();` part is based on the assumption that this is in a single
   post template (single.php).
 * for a category archive page, try to rewrite the code to:
 *     ```
       <h1 id="logo">
       <?php
       $in_news = 0; // set a flag varable to false
       if( in_array( '23', get_ancestors( get_query_var('cat'), 'category' ) ) ) $in_news = 1; // set flag to true
   
       if( $in_news == 1 || is_category(23)) : ?>
       <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo-old.png"></a>
       <?php else : ?>
       <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo.png"></a>
       <?php endif; ?>
       </h1>
       ```
   
 * n
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130956)
 * I’m using this in header.php so it will be global throughout all pages on my 
   site.
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130962)
 * I should clarify, I’m using this in header.php but I need it to apply to single
   post pages, where you are seeing the permalink for a blog post.
 * I need it to detect if the article has the parent news category.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130963)
 * > I’m using this in header.php so it will be global throughout all pages on my
   > site.
 * however, the only two scenarios, where this makes sense, is a single post, or
   a category archive;
 * possibly you need to combine both codes:
 * example:
    [http://pastebin.com/BAXjA1jj](http://pastebin.com/BAXjA1jj)
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130969)
 * That code works for the category pages, but not for the single post pages.
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130970)
 * PS – I did change the 20 to 23 in your code for single post as that’s the correct
   ID for my news category, however it’s still not working.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130971)
 * as i copied the code of [@esmi](https://wordpress.org/support/users/esmi/)’s 
   reply, i might hav ethe wrong cat id in this line (line 8):
 *     ```
       if( in_array( '20', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true
       ```
   
 * please have a look and correct it if neccessary.
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130972)
 * Yes, I did catch that the cat id was off. No luck on the single post pages yet…
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130973)
 * my bad – i missed to use the category ids from the single post for the comparison:
   (
   in line 8) `if( in_array( '23', get_ancestors( $cat->term_id, 'category' ) ) )
   $in_news = 1; // set flag to true`
 * new pastebin: [http://pastebin.com/Y7fS27c6](http://pastebin.com/Y7fS27c6)
 *  Thread Starter [foochuck](https://wordpress.org/support/users/foochuck/)
 * (@foochuck)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130974)
 * I was just printing out the array and saw that too…thanks very much!

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

The topic ‘Conditional for parent category’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 3 participants
 * Last reply from: [foochuck](https://wordpress.org/support/users/foochuck/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/conditional-for-parent-category/#post-2130974)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
