自定义wordpress函数没有显示正确的帖子数

问题描述:

我有一个自定义wordpress函数,显示特色食谱的数量。当查询中有showposts=-1时,该功能正常工作。然而,当我把'showposts = 5'只显示我的两个帖子。自定义wordpress函数没有显示正确的帖子数

下面是我的功能

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

在这个页面上https://codex.wordpress.org/Class_Reference/WP_Query我看到posts_per_age已经取代showposts参数开始WP 2.1,所以只是好奇,是什么版本的WP的你在 – Satya

+0

@Satya im 4.2版 – user38208

+0

@Satya,'showposts' [still works](https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/query.php#L2463 ) – rnevius

也许是因为只有2个(最后5个职位,你实际上调用)得到

get_field('featured-pika-recipe') == 'Yes'

你可以尝试直接添加这个自定义字段的查询来获取最后5帖与此自定义场下面的代码

'meta_key'  => 'featured-pika-recipe', 
'meta_value' => 'Yes' 
+0

这可以在我删除if语句时起作用 – user38208

+0

以下页面可以帮助您将自定义字段添加到wordpress查询中http://www.advancedcustomfields.com/resources/query-posts-自定义字段/ –