Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to include all subcats in conditional tags WordPress

I have a simple question, how do you include all subcats in conditional tags?

For example, here's my code:


<?php if (in_category ('videos')) { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-video-overlay.png"/></a></h3>
<?php } elseif(in_category ('photos')) { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-photos-overlay.png"/></a></h3>
<?php } elseif(in_category ('music')) { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-music-overlay.png"/></a></h3>
<?php } elseif(in_category ('opinions')) { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-opinion-overlay.png"/></a></h3>
<?php } elseif(in_category ('interviews')) { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-interview-overlay.png"/></a></h3>
<?php } else { ?>
<h3><a href="<?php the_permalink(); ?>"><img alt="" src="<?php bloginfo("template_url"); ?>/images/exclusive-interview-overlay.png"/></a></h3>
<?php } ?>




For the code above, it only show the h3 tag for my "videos" category, but won't for the subcategory "music videos" so on and so on.

Obviously, I can create another line and add "music videos" in the same way I have "videos" in the line above, but I'm trying to eliminate having to do this. There has to be a way where I can just keep the code pretty much the same as it is above but somehow include all subcategories.

Edit: I also know about the arrays as well to include each category but was wondering if there is an alternative method than me having to look each category name up and type it in my code. I have some categories with a ton of subcats. and this doesn't seem feasible to me.

Does anyone know how to do this?

I know that this is a simple question, just haven't found answer yet and I don't mind giving up a couple of easy bucks to have someone just show me the way and answer a question they probably know like the back of their hand. :-)

Answers (3)

2011-09-29

Luis Abarca answers:

You can pass an array with the categories ID's or slug's


if (in_category(array('videos', 'music')))


Brennen Jones comments:

I had to edit my question because I already knew about this way. I was wondering if there is an alternative method than me having to look each category name up and type it in my code. I have some categories with a ton of subcats. and this doesn't seem feasible to me.


Brennen Jones comments:

For example, I have a category with like 25 subcategories, so based on the array method if I was to include all 25 categories, I would have to spell each one out.

Do you know what I'm saying?


Luis Abarca comments:

You are using this code on a archive ?? or post ?



$categories= get_categories('child_of=1&hide_empty=0');
$slugs = array();

foreach ($categories as $category) {
$slugs[] = $category->slug;
}


<?php if (in_category($slugs) ) { ?>



Brennen Jones comments:

Nope that's not the code I'm using

2011-09-29

Abdessamad Idrissi answers:

Check this out :)
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;
}
}
$idObj = get_category_by_slug('videos');
$id = $idObj->term_id;

<?php if (in_category ('videos') || post_is_in_descendant_category( $id )) { ?>


Brennen Jones comments:

do i put the first part of your code in the functions.php file?


Abdessamad Idrissi comments:

yes :) you can put the function post_is_in_descendant_category in functions.php and leave the rest in the current file.
you can also leave it here in the file, but for clean code it's better to put it in the functions.php

2011-09-30

Romel Apuya answers:

here's a related question to this..with its answer..

[[LINK href="http://wpquestions.com/question/show/id/1979"]]http://wpquestions.com/question/show/id/1979[[/LINK]]