1

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?

3

I think u may looking for,

 $ignoredcats = array(2,360,124);
 if (in_array($_category_id, $ignoredcats )){
   //do nothing
 }
 else{
   //do something
 }
  • Could have sworn I tried this actually - either way - correct answer and this works - thanks. Accepted this answer as just pipped @Marius's. – zigojacko Mar 4 '14 at 13:18
2

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 } ?>
  • Could have sworn I tried this actually - either way - correct answer and this works - thanks. Accepted @Helphin's answer as just pipped yours first. – zigojacko Mar 4 '14 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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