【问题标题】:Show post if true/false is yes. Advanced custom fields如果真/假为是,则显示帖子。高级自定义字段
【发布时间】: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(); ?>

【问题讨论】:

标签: php wordpress advanced-custom-fields


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    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 
    }
    

    确保为单选按钮保存到数据库中的值与您在 if 语句中给出的值一样

    这是您在查询中使用元值的可选选项。如果您不使用,您可以借助从Wp_Query 循环中获得的帖子 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 语句似乎不正确,我已将 Query Executed 输出添加到该 if 语句中。

    【讨论】:

    • 嗨纳雷什。我对您之前的帖子进行了一些编辑以删除报价设备 (&gt;)。这仅适用于引用,即来自其他来源的内容,如手册、错误消息或演讲。它不是一般的荧光笔,如果这样使用会令人困惑。谢谢!
    • 感谢您:我尝试使用新循环,但它不起作用。你能告诉我你的整个循环吗?李
    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2021-02-11
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多