Correct syntax for multiple category ID's in array
Question
Trying to exclude the rendering of something based on category ID using the following...
<?php if ($_category_id == 2) { ?>
<?php //do nothing ?>
<?php } else { ?>
<?php //do something ?>
<?php } ?>
I need to specify multiple category ID's for exclusion however so I gather these need to be loaded into an array.
<?php $ignoredcats = array(2,360,124); ?>
<?php if (count(array_intersect($ignoredcats,$_category_id))) { ?>
<?php //do nothing ?>
<?php } else { ?>
<?php //do something ?>
<?php } ?>
This doesn't work though. How should I be specifying by multiple category ID please?
Solution
I think u may looking for,
$ignoredcats = array(2,360,124);
if (in_array($_category_id, $ignoredcats )){
//do nothing
}
else{
//do something
}
OTHER TIPS
Try using in_array()
.
<?php $ignoredcats = array(2,360,124); ?>
<?php if (in_array($_category_id, $ignoredcats)) { ?>
<?php //category is in $ignoredcats ?>
<?php } else { ?>
<?php //category is not in $ignoredcats ?>
<?php } ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange