skip to Main Content

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


  1. 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).

    Login or Signup to reply.
  2. 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 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 ( 'yes' === get_field( 'reservations' ) && 'yes' === get_field( 'payments' ) ) { ?>
    
        <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 } else { 
        // Use else here since there are no more conditions.
        ?>
    
        <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 } ?>
    

    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 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 ( 'yes' === get_field( 'reservations' ) && 'yes' === get_field( 'payments' ) ) : ?>
    
        <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 else : ?>
    
        <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 endif; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search