If else PHP using the_field() is not working

Viewed 77

I created a custom field to indicate a featured resource. It is just a text box you can type "yes" if it is featured. I'd like the featured resource to display first before the other resources in the loop. When I run the code, nothing in the "if" runs at all, but the "else" displays as planned. If I use the_field() instead, a "yes" appears next to the correct resource so I think the custom field is set up correctly.

<?php 
while ( have_posts() ) : the_post(); 
    $featured = get_field('featured');
    if ( $featured  == 'yes' ):
?>
        <div class="four-column-inner notched homepage-card is-layout-flow wp-block-column library featured">
            <h3 style="font-size:22px; line-height: 1.5em;">
                Featured Resource: <?php the_title(); ?>
            </h3>
            <div> <?php the_post_thumbnail() ?> </div>
            <p>Categories:</p>
            <p style="margin-top:0;">
<?php 
        $terms = the_terms(get_the_ID(),
                        array(
                         'building_type',
                         'lease_phase',
                         'topic',
                         'building_stakeholder')
                        ); 
?>
            </p>
            <a href="<?php the_permalink(); ?>"><button>View Resource</button></a>
        </div>
<?php 
    else: 
?>
        <div class="four-column-inner notched homepage-card is-layout-flow wp-block-column library">
            <h3 style="font-size:22px; line-height: 1.5em;">
                <?php the_title(); ?>
            </h3>
            <div> <?php the_post_thumbnail() ?> </div>
                <p>Categories:</p>
                <p style="margin-top:0;">
<?php $terms = the_terms(
                        get_the_ID(),
                        array(
                         'building_type',
                         'lease_phase',
                         'topic',
                         'building_stakeholder')
                        ); 
?>
                </p>
                <a href="<?php the_permalink(); ?>"><button>View Resource</button></a>
            </div>
        
    <?php endif ?>
<?php endwhile ?>
1 Answers

Try to read the value of $featured by adding this

if( $featured ) {
    echo $featured ;
} else {
    echo 'empty';
}
Related