Reputation: 45
hope you can help.
I need to do the following, this is using Advanced Custom Fields in Wordpress 4.8
Display an Button if
A: The Event Date has not passed (i.e. the Event is finished)
AND
B: The Buy Ticket Link field has a value in it.
I have succeeded with A using the code below but I cannot work out how to combine this with B, using ACF.
I have looked at the ACF support post about hiding fields and the page is here: https://www.advancedcustomfields.com/resources/hiding-empty-fields/ but I don't have the skills to combine these two. I would hugely appreciate any help here please.
<?php
$eventDate = get_field('event_date', false, false);
$today = (date('Y-m-d'));
?>
<?php if ($eventDate >=$today) { ?>
<div class="tickets">
<a class="btn-primary" href="<?php echo the_field('buy_link') ?>" target="_blank">
Buy Tickets</a>
</div>
<?php
} else { ?>
<div class="tickets">
<div class="btn-primary" href="" target="_blank" style="color: white">
This is an old event. Tickets are no longer on sale.</div>
</div>
<?php } ?>
Upvotes: 0
Views: 360
Reputation: 2492
Don't overcomplicate it -
If you want to have an extra condition, you can specify it in your conditional statement.
<?php
$eventDate = get_field('event_date', false, false);
$today = (date('Y-m-d'));
$ticketLink = get_field('buy_link');
?>
<?php if ($eventDate >= $today && $ticketLink) { ?>
<div class="tickets">
<a class="btn-primary" href="<?php echo get_field('buy_link') ?>" target="_blank">
Buy Tickets</a>
</div>
<?php
} else { ?>
<div class="tickets">
<div class="btn-primary" href="#" style="color: white">
This is an old event. Tickets are no longer on sale.</div>
</div>
<?php } ?>
Read more about your logical operators here : Logical Operators
Upvotes: 1