如何仅显示自定义帖子类型的精选帖子?

问题描述:

早上好,如何仅显示自定义帖子类型的精选帖子?

我创建了一个自定义帖子类型 - 产品 - 并为该自定义帖子类型创建了一个自定义字段 - featured_product - 使用高级自定义字段插件。 当我创建自定义字段时,我使用了True/False字段类型。

我想显示这些产品帖子,其中的feature_product复选框 被选中。

这是我当前的代码:

<?php query_posts(array(
'posts_per_page' => 3, 
'post_type' => 'products', 
'orderby' => 'post_date', 
'paged' => $paged 
) 
); ?> 

<?php if (have_posts()) while (have_posts()) : the_post(); ?> 

<?php if(get_field('featured_product')){ ?> 

<div id="post-<?php the_ID(); ?>" class="cpt"> 
<h2><?php the_title(); ?></h2> 
<?php 
if (has_post_thumbnail()) { 
    the_post_thumbnail('excerpt'); 
} 
?> 
<?php the_excerpt(); ?> 
<ul class="prod_detail"> 
<li><a href="<?php the_field('product_detail_page'); ?>">Learn More</a></li> 
<li><a href="<?php the_field('purchase_link'); ?>">Place Order</a></li> 
</ul> 
</div> 

<?php } ?> 

<?php endwhile; ?> 

<?php wp_reset_query(); ?> 

的问题是,它只返回一个职位 - 但我有3个职位检查为被功能。

我在这里做错了什么?

非常感谢!

我想通了:)

<?php query_posts(array(
'posts_per_page' => 3, 
'post_type' => 'products', 
'orderby' => 'post_date', 
'meta_key' => 'featured_product', // the name of the custom field 
'meta_compare' => '=', // the comparison (e.g. equals, does not equal, etc...) 
'meta_value' => 1, // the value to which the custom field is compared. In my case, 'featured_product' was a true/false checkbox. If you had a custom field called 'color' and wanted to show only those blue items, then the meta_value would be 'blue' 
'paged' => $paged 
) 
); ?> 

<?php if (have_posts()) while (have_posts()) : the_post(); ?> 

<div id="post-<?php the_ID(); ?>" class="cpt"> 
<h2><?php the_title(); ?></h2> 
<?php 
if (has_post_thumbnail()) { 
    the_post_thumbnail('excerpt'); 
} 
?> 
<?php the_excerpt(); ?> 
<ul class="prod_detail"> 
<li><a href="<?php the_field('product_detail_page'); ?>" target="_blank">Learn More</a></li> 
<li><a href="<?php the_field('purchase_link'); ?>" target="blank">Place Order</a></li> 
</ul> 
</div> 

<?php endwhile; ?> 

<?php wp_reset_query(); ?>