【发布时间】:2017-01-08 08:44:00
【问题描述】:
我已经设法让我的循环仅显示在高级自定义字段上显示为 true 的帖子。
但我现在只想显示一篇文章。我似乎无法让它只循环其中一个以 true/false 字段为 yes 的帖子。
'posts_per_page' => '1'
不起作用,因为它只显示最新的帖子。如果没有勾选,它只会显示空白。
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
【问题讨论】:
-
回复了下面的答案。实施这一点并分享想法。以及在此处添加答案。 stackoverflow.com/questions/39276798/….
标签: php wordpress advanced-custom-fields