Here’s a very basic example that should do what you need it to do. Basically, we get all of the posts in your custom post type, then loop through them individually. When we find the one set with “promote_to_homepage,” we render some display and exit the loop:
// Query for all posts in the post type "vendors"
query_posts( 'post_type=vendors' );
$exitloop = false;
// Start the loop
while ( have_posts() ) : the_post();
// Is this post the featured post?
$featured = get_field( 'promote_to_homepage', $post->ID, true ) ;
if ( $featured ) { ?>
// This is our featured post, so display it and set $exitloop so we quit
$exitloop = true;
<div id="vendor-img">
<img src="https://wordpress.stackexchange.com/questions/53062/<?php the_field("vendor_logo'); ?>"/>
</div>
<h5><?php the_title(); ?></h5>
<?php }
if ( $exitloop ) break;
endwhile;
There are more elegant ways to do this, though. There is no get_field()
function in WordPress core, so I’m assuming you’re using the Advanced Custom Fields plugin. If so, this field is stored in a meta key and can also be retrieved with:
get_post_meta( $post->ID, '_promote_to_homepage', true );
This means you can perform a custom query to select just 1 post that contains that particular meta key:
$args = array(
'post_type' => 'vendors',
'posts_per_page' => 1,
'meta_query' => array( array(
'key' => '_promote_to_homepage',
'value' => true,
'compare' => '='
) )
);
$custom_query = new WP_Query( $args );
// Start the loop
while ( $custom_query->have_posts() ) : $custom_query->the_post();
?>
<div id="vendor-img">
<img src="https://wordpress.stackexchange.com/questions/53062/<?php the_field("vendor_logo'); ?>"/>
</div>
<h5><?php the_title(); ?></h5>
<?php
endwhile;