dungey_140 dungey_140 - 10 months ago 71
PHP Question

PHP 'not equal to' multiple categories

I'm trying to target all categories in WooCommerce that aren't 'Coffee','Subscription for Me' or 'Gift Vocuhers'. So, any category apart from these three should display the


The code below works when I target any category apart from 'Coffee':

<? if ($categoryName != "coffee") : ?>
<h3><? echo $product["title"]; ?></h3>
<? endif; ?>

However, when I try to target eliminate multiple categories, it doesn't work. How can I adjust the first piece of code to allow multiple categories to be ignored?

My attempt (not working):

<? if ($categoryName != "coffee" || "subscription-for-me" || "gift-vouchers") : ?>
<h3><? echo $product["title"]; ?></h3>
<? endif; ?>


Answer Source

A more extendable version and possibly cleaner could be to use in_array() like so:

<? if (!in_array($categoryName, ["coffee", "subscription-for-me", "gift-vouchers"])): ?>
    <h3><?php echo $product["title"]; ?></h3> 
<? endif; ?>