0

I’m trying to conditionally display a div if a post object post has a specific custom taxonomy term. The custom taxonomy is called ‘room_vacancy’ and the term is called ‘booked’

I’m using the following code but it doesn’t work:

<?php $post_objects = get_field('bedrooms_object');

if( $post_objects ): ?>
<h2>Bedrooms</h2>

    <ul class="bedrooms">
    <?php foreach( $post_objects as $post_object): ?>
            <li class="bedroom">
                        <div class="inner row">

                        <div class="col-md-5 image">
                            <?php if ( is_tax( 'room_vacancy', 'booked') ) { ?>
                                <div class="label">Booked</div>
                            <?php } else { ?>
                            <?php } ?>
                            <img class="lazy" src="<?php echo get_the_post_thumbnail_url($post_object->ID, 'large'); ?>"/>
                            <div class="details">
                                <div class="price">Price <span><?php the_field('room_price', $post_object->ID); ?><span></div>
                                <div class="size">Size: <span><?php the_field('room_size', $post_object->ID); ?></span></div>
                            </div>
                        </div>
                        <div class="col-md-7 content">
                            <h2><?php echo get_the_title($post_object->ID); ?></h2>

                            <?php the_field('listing_content', $post_object->ID); ?>
                            <a class="btn" href="<?php the_field('button_url', $post_object->ID); ?>">Apply Now</a>

                        </div>
                    </div>

                </li>
    <?php endforeach; ?>
    </ul>
<?php endif;?>

Any advice on how to do this?

1 Answer 1

0

Instead of is_tax(), which

Determines whether the query is for an existing custom taxonomy archive page.

you should use has_term(),

Check if the current post has any of given terms.

Like so,

if ( has_term( 'booked', 'room_vacancy', $post_object ) ) {
  // Yay!
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.