How to put an array in is_category

The question:

I try to put an array in a is_category():

I have my array like this

$term_id = 7;
$taxonomy_name = 'category';
$termchildren = get_term_children( $term_id, $taxonomy_name);
$mycategory= array();
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name);
$mycategory[] = $term->term_id; 
}


$mycategory =     Array ( [0] => 23 [1] => 33 [2] => 37 [3] => 54 [4] => 56 [5] => 58 [6] => 60 [7] => 62 [8] => 64 [9] => 66 [10] => 68 [11] => 70 [12] => 72 [13] => 74 [14] => 76 )

Now when i put my code

if(is_category(array($mycategory))):
//echo 'it's work';
else:
//echo 'nope';
endif;

It’s don’t work

Thanks for help

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Try:

$mycategory = array(23, 33, 37);

if( is_category( $mycategory ) ) {
   echo 'yes';
} else {
   echo 'no';
}

is_category returns true if there are any matches.

You could try this:

... 
$term = get_term_by( 'id', $child, $taxonomy_name);
if ( is_category( $term->term_id ) ) {
   $mycategory[] = $term->term_id;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment