繁体   English   中英

如果是/否,则显示帖子。 高级自定义字段

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

我设法使我的循环只显示在高级自定义字段上显示为true的帖子。

但是我现在只想显示一个帖子。 我似乎无法让它只循环播放具有true / false字段的帖子之一,是。

'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(); ?>

ACF单选按钮是/否字段将以其他方式操作。 您必须获取输出,然后将其与所需的值进行比较。

句法:

$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 
}

确保保存到单选按钮的数据库中的值与if语句中给出的值相同

这是可选的,您可以在查询中使用meta_value。 如果您不使用,则可以在Wp_Query循环中获取的发布ID的帮助下获取acf值

如果您只需Wp_Query一个帖子发布到已存储的最新帖子,则可以像这样更改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语句似乎不正确,我已将Query Executed输出添加到该if语句中。

您应该在此处使用元查询。 将您的args数组更改为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM