dungey_140 dungey_140 - 1 month ago 15
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

<h3>
tag.

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; ?>


Thanks

Answer

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; ?>
Comments