简体   繁体   中英

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

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

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'

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

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.

Syntax:

$variable = get_field('field_name', $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 
}

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

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

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

Note:

post_per_page=10 -> Will bring the 10 posts from your post type

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

Entire Loop:

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

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

You should use Meta Query here. Change your args array to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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