最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

If ACF Post Object post has custom taxonomy term…

programmeradmin2浏览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?

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?

Share Improve this question asked Nov 20, 2019 at 15:14 Ciaran GaffeyCiaran Gaffey 911 gold badge3 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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!
}
发布评论

评论列表(0)

  1. 暂无评论