如果true/false为yes,则显示post.高级自定义字段


Show post if true/false is yes. Advanced custom fields

我已经设法让我的循环只显示在高级自定义字段上显示为真的帖子。

但是我现在只想显示一个帖子。我似乎无法让它只循环一个具有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(); ?>

你应该在这里使用元查询。将args数组更改为:

// args
$args = array(
    'numberposts'   => 1,
    'post_type'     => 'event',
    'posts_per_page' => '1'
    'meta_key'      => 'sponsored_event',
    'meta_value'    => 'yes'
);

ACF单选按钮Yes/No字段将以另一种方式操作。您必须获得输出,然后与您需要的值进行比较。

语法:

$variable = get_field('field_name', $post->ID);

其中$post->ID将从您使用的循环中出现。

if (get_field('sponsored_event') == 'yes') {
    // code to run if the above is true
} 
else
{
     // code for else part 
}

确保保存到DB中的单选按钮的值与if语句中的值相同

在查询中使用meta_value是可选的。如果您不使用,您可以通过从Wp_Query

循环中获得的post ID来获取acf值。

如果您只需要一篇文章,那么将Wp_Query更改为已存储的最新一篇。

$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');

如果你想要所有已经存储的文章,你可以像这样使用

$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');

注意:

post_per_page=10 ->将从您的帖子类型中取出10篇文章

post_per_page=-1 ->将显示您的帖子类型包含的无限个帖子

整个循环:

<?php
$args = array(
  'posts_per_page'  => 1,
  'post_type'       => 'event',
  'meta_key'        => 'sponsored_event',
  'meta_value'  => 'yes'
);
$the_query = new WP_Query( $args );
?>  
        <?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <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 endwhile; else: ?>
    <?php endif; ?>
<?php wp_reset_query(); ?>

查询中的If语句似乎是不正确的,我已经将查询执行的输出添加到该If语句。