1

I'm using conditional statements on my archives pages to display a different content depending of my categories.

I have 2 parent categories :

author collections

each parent categorie has children categories

author-1 author-2 and so on...

on my archive.php here is my code :

<?php if(is_category("collections")) : ?>

/* CONTENT FOR PARENT COLLECTION */

<?php endif; ?>

what I want is to be abble to target if its in a sub category of authors for example.

something like this :

<?php if(is_category("collections")) : ?>

/* CONTENT FOR PARENT COLLECTION */

<?php elseif(is_category("author")) : ?>

/* CONTENT FOR PARENT AUTHOR */

<?php elseif (*is_children_category_of*("collections")) ?>

/* CONTENT FOR CHILDREN COLLECTIONS */

<?php elseif (*is_children_category_of*("author")) ?>

/* CONTENT FOR CHILDREN AUTHORS */

<?php endif; ?>

I can't find any answer, does anybody know how to do this ?

thanks

1
  • Did the answer below solve your problem?
    – jdm2112
    Commented Jan 23, 2018 at 15:17

1 Answer 1

2

You are looking for the cat_is_ancestor_of() function. https://codex.wordpress.org/Function_Reference/cat_is_ancestor_of

The example in the WP codex is very close to what you are attempting for your archive template.

<?php 
  // if the category is music or a music SUBcategory, 
   if (cat_is_ancestor_of( 4, $cat ) or is_category( 4 ) ):  ?>
   <div id="music_subnav_menu" class="subnav_menu">
    <?php wp_nav_menu( array( 'menu' => 'Music' ) ); ?>
  </div>
 <?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.