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 style="padding:28px 20px 0 20px ; min-height:109px">
<a href="<?php the_field('payments_link'); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a 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 style="padding:28px 20px 0 20px ; min-height:109px">
<a 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 style="padding:28px 20px 0 20px ; min-height:109px">
<a 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 style="padding:28px 20px 0 20px ; min-height:109px">
<a 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 style="padding:28px 20px 0 20px ; min-height:109px">
<a href="<?php the_field('payments_link'); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a 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 style="padding:28px 20px 0 20px ; min-height:109px">
<a 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
CodePudding user response:
Here :
<?php if (get_field('reservations') == 'yes' && get_field('payments') == 'no' { ?>
You missed the closing parenthesis of the if
before {
:
<?php if (get_field('reservations') == 'yes' && get_field('payments') == 'no') { ?>
It's the same for all your elseif
statements.
Also, I recommend you to use this syntax :
<?php if (get_field('reservations') == 'yes' && get_field('payments') == 'no') : ?>
// code ...
<?php elseif(condition) : ?>
// code ...
<?php endif; ?>
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).
CodePudding user response:
In addition to your mistake of not closing the parenthesis on your if
and elseif
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:
<?php if ( 'yes' === get_field( 'reservations' ) && 'no' === get_field( 'payments' ) ) { ?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'reservations_link' ); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php } elseif ( 'yes' === get_field( 'reservations' ) && 'yes' === get_field( 'payments' ) ) { ?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'payments_link' ); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a href="<?php the_field( 'reservations_link' ); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php } else {
// Use else here since there are no more conditions.
?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'payments_link' ); ?>" style="text-decoration:none" target="_blank">Make a Payment</a>
</div>
<?php } ?>
Whether you convert to using a :
or {
as previously suggested is up to you. It does provide for some additional readability.
<?php if ( 'yes' === get_field( 'reservations' ) && 'no' === get_field( 'payments' ) ) : ?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'reservations_link' ); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php elseif ( 'yes' === get_field( 'reservations' ) && 'yes' === get_field( 'payments' ) ) : ?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'payments_link' ); ?>" style="text-decoration:none ; margin-bottom:8px" target="_blank">Make a Payment</a>
<a href="<?php the_field( 'reservations_link' ); ?>" style="text-decoration:none" target="_blank">Reserve Your Spot</a>
</div>
<?php else : ?>
<div style="padding:28px 20px 0 20px; min-height:109px">
<a href="<?php the_field( 'payments_link' ); ?>" style="text-decoration:none" target="_blank">Make a Payment</a>
</div>
<?php endif; ?>