I am using ACF Pro on a client site and am running into a problem. In my field group I have a series of 4 fields:
Accept Reservations? (field name ‘reservations’ – radio – yes/no)
Reservations Link (field name ‘reservations_link’ – URL field)
Accept Payments? (field name ‘payments’ – radio – yes/no)
Payments Link (field name ‘payments_link’ – URL field)
What I am trying to do is display a bit of code depending on the combination of the radio button values. For example:
If "Accept Reservations" = Yes AND "Accept Payments" = Yes then display:
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('payments_link'); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a class="button-reserve cta" href="<?php the_field('reservations_link'); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
If "Accept Reservations" = Yes AND "Accept Payments" = No then display:
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('reservations_link'); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
If "Accept Reservations" = No AND "Accept Payments" = Yes then display:
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('payments_link'); ?>" style="text-decoration:none" target="_blank">Make a Payment</a>
</div>
The problem is that the PHP code I am using in the template file not only doesn’t work, but it breaks the page. Here is my PHP code block for all of the above:
<?php if (get_field('reservations') == 'yes' && get_field('payments') == 'no' { ?>
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('reservations_link'); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php } elseif (get_field('reservations') == 'yes' && get_field('payments') == 'yes' { ?>
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('payments_link'); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a class="button-reserve cta" href="<?php the_field('reservations_link'); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php } elseif (get_field('reservations') == 'no' && get_field('payments') == 'yes' { ?>
<div class="wp-block-column" style="padding:28px 20px 0 20px ; min-height:109px">
<a class="button-reserve cta" href="<?php the_field('payments_link'); ?>" style="text-decoration:none" target="_blank">Make a Payment</a>
</div>
<?php } ?>
What am I doing wrong? Any help would be appreciated.
Best,
Cynthia
2
Answers
Here :
You missed the closing parenthesis of the
if
before{
:It’s the same for all your
elseif
statements.Also, I recommend you to use this syntax :
It’s more readable, and you don’t have some closing brackets lost in your code that you don’t know if it’s related to a if, for, foreach etc.
Another recommendation would be to test smaller pieces of code. You seemed to have code all this block of code without testing it at each step. Take it step by step, and test your
if
statement first. If it works, continue, else you have a small piece of code to debug. PHP should tell you what’s wrong. (Sometimes it’s not the line indicated that is wrong, but the previous, like when you forget a semi colon or closing parenthesis).In addition to your mistake of not closing the parenthesis on your
if
andelseif
statements, you should use strict comparisons when comparing the values of variables. Strict comparisons===
instead of==
is safer code, and also recommended by WordPress php coding standards. Your refined code should look like this:Whether you convert to using a
:
or{
as previously suggested is up to you. It does provide for some additional readability.