Php 自定义wordpress函数未显示正确的帖子数
我有一个自定义的wordpress函数,可以显示特色食谱的数量。当查询中有Php 自定义wordpress函数未显示正确的帖子数,php,wordpress,Php,Wordpress,我有一个自定义的wordpress函数,可以显示特色食谱的数量。当查询中有showposts=-1时,该函数工作正常。然而,当我输入'showposts=5'时,只显示了我的两篇文章 下面是我的功能 function pika_featured_recipes_shortcode() { ob_start(); echo '<div class="featured-box-heading"> Featured Recipes </div>';
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' );
删除此项和帖子的数量看起来很好。有什么办法可以解决这个问题吗?可能是因为只有2篇帖子(在你实际调用的最后5篇帖子中)得到了回复
get_字段('featured-pika-recipe')=='Yes'
您可以尝试将此自定义字段直接添加到查询中,以使用此自定义字段获取最后5篇文章,代码如下
'meta_key' => 'featured-pika-recipe',
'meta_value' => 'Yes'
在这个页面上,我看到posts_per_age已经取代了以wp v2.1开头的showposts参数,所以我很好奇您是wp的哪个版本on@Satya我是第四版的。2@Satya,
showposts
是的,不会有。我重新检查了源代码,注意到WP在内部转换了这个案例。好的,问题似乎是“如果(获取字段('featured-pika-recipe')='Yes'){”删除这个,帖子的数量会显示得很好。下一页可以帮助您将自定义字段添加到wordpress查询中
'meta_key' => 'featured-pika-recipe',
'meta_value' => 'Yes'