I’m trying to only proceed past the first IF statement if there is content in the sub field called INSTRUCTION, which seems to not work. It works if I remove && !empty(the_sub_field(‘instruction’)

<?php

if( have_rows('instructions_group') && !empty(the_sub_field('instruction')) ): 

    ?>

    <div id="instructions-group0" class="instructions-group">                
    <h2>DIRECTIONS:</h2>
    <ol>

<?php   

    if( have_rows('instructions_group') ):

        while ( have_rows('instructions_group') ) : the_row();

        $instructionphoto = get_sub_field('instruction_photo');
        $instruction = get_sub_field('instruction');
        $size="recipe-featured";
        $thumb = $instructionphoto['sizes'][ $size ];
        $width = $instructionphoto['sizes'][ $size . '-width' ];
        $height = $instructionphoto['sizes'][ $size . '-height' ];
        $alt = $instructionphoto['alt'];

        if( !empty($instruction) ) : ?>


            <li class="instruction">

                <div class="instruction-wrap">

                    <?php if( !empty($instructionphoto) ) : ?>

                        <img src="https://wordpress.stackexchange.com/questions/341782/<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />

                    <?php endif; ?>

                    <div class="instruction-txt"><?php echo $instruction; ?></div>

                </div>

            </li>

        <?php 

        endif;

        endwhile;

         ?>

        </ol>
    </div>

    <?php   

    endif; 

endif;

?>

1 Answer
1

As seen on ACF documentation, to check for a value before displaying it, simply use:

<?php if( get_field('field_name') ): ?>
    <p>My field value: <?php the_field('field_name'); ?></p>
<?php endif; ?>

So in your case, you are already getting the sub_field into $instruction, so simply use this:

 if( $instruction) ) : 

Leave a Reply

Your email address will not be published. Required fields are marked *