我有一个自定义wordpress功能,显示特色食谱的数量。查询中showposts=-1
时,该函数正常工作。但是当我把'showposts = 5'时,我的帖子只显示了两个。
以下是我的功能
function pika_featured_recipes_shortcode() {
ob_start();
echo '<div class="featured-box-heading"> Featured Recipes </div>';
echo '<div class="featured-box">';
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5&post_type=cp_recipe&order=Desc&orderby=date' . '&paged=' . $paged);
while ($wp_query->have_posts()):
$wp_query->the_post();
$entry_id = get_the_ID();
$entry_link = get_permalink($entry_id);
$entry_image = get_post_thumbnail_id($entry_id);
$entry_title = get_the_title($entry_id);
$entry_description = get_post_meta($entry_id, '_cp_recipe_short_description', true);
$entry_excerpt = get_post_meta($entry_id, '_cp_recipe_excerpt', true);
$likes = get_post_meta( $entry_id, '_cp_likes', true );
if (get_field('featured-pika-recipe') == 'Yes') {
echo '<div class="featured-result-box item ">
<div class="box">
<div class="pika-featured-box-img">';
if(!empty($entry_image)) {
echo '<a href="'.$entry_link.'">'.wp_get_attachment_image($entry_image, 'cp_298_192').'</a>';
}
echo' </div><!-- /.box-img -->';
echo'<div class="box-entry">
<h5 class="pika-featured-title"><a href="';
echo $entry_link;
echo '">';
echo $entry_title;
echo'</a></h5>';
echo $trimmed = wp_trim_words( $entry_description, $num_words = 15, $more = null );
echo'</div><!-- /.box-entry -->';
echo'</div>';
echo'</div>';
echo'<div style="clear:both"></div>';
}
endwhile;
wp_reset_query();
echo '</div>';
$output = ob_get_clean();
return $output;}
add_shortcode( 'pika_featured-recipes', 'pika_featured_recipes_shortcode' );
问题似乎与
有关if (get_field('featured-pika-recipe') == 'Yes') {
删除此帖子和帖子数量似乎很好。我能解决这个问题吗?
答案 0 :(得分:1)
也许是因为只有2个帖子(你实际调用的最后5个帖子)
get_field('featured-pika-recipe') == 'Yes'
您可以尝试将此自定义字段直接添加到您的查询中,以使用以下代码获取此自定义字段的最近5篇帖子
'meta_key' => 'featured-pika-recipe',
'meta_value' => 'Yes'