如果是/否,则显示帖子。高级自定义字段 [英] Show post if true/false is yes. Advanced custom fields

查看:82
本文介绍了如果是/否,则显示帖子。高级自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have managed to get my loop to show only the posts that display true on an advanced custom field.

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

But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.

'posts_per_page'=>'1'

'posts_per_page' => '1'

不起作用,因为它仅显示最新的帖子。如果未选中,则仅显示空白。

Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.

<?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单选按钮是/否字段为用另一种方式操纵。您必须获取输出,然后将其与所需的值进行比较。

The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.

语法:

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

$ post-> ID将在您使用的循环中出现的位置。

Where the $post->ID will be appering from the Loop that you use.

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

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

Make sure that the value saved into the DB for the radio button is like you give in the if statement

这对您来说是可选的在查询中使用meta_value。如果您不使用,则可以借助从 Wp_Query

This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query

如果只需要将一个帖子发布到已存储的最新帖子,则可以像这样更改 Wp_Query

Change the Wp_Query like this if you need only one post that to the latest one that has been stored.

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

否则,如果要存储所有帖子,可以这样使用。

Else if you want all the posts that has been stored you can use like this.

$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=10 -> Will bring the 10 posts from your post type

post_per_page = -1->将带您帖子类型具有的无限帖子

post_per_page=-1 -> will bring infinite posts that your post type has

整个循环:

<?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输出如果声明。

Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.

这篇关于如果是/否,则显示帖子。高级自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆