Search code examples
phpwordpresswoocommercewordpress-themingwoocommerce-theming

Woocommerce how to use ternary operator to display featured products


I have added the following code to display "Featured: yes/no":

<div class="event-package-content">
    Featured: <?php echo $product->get_event_listing_featured(); ?>
</div>

I would like to change it to display only the word "Featured" under the package if the package is featured instead of "yes/no".

Package screenshot:

Package screenshot

Packages (shows after filling in details)

Can anyone please help?

Thanks.


Solution

  • You could replace your code:

    <div class="event-package-content">
        Featured: <?php echo $product->get_event_listing_featured(); ?>
    </div>
    

    With this:

    <div class="event-package-content">
      <?php echo ($product->get_event_listing_featured() == "yes") ? "Featured": ""; ?>
    </div>