2015-06-20 65 views
1

我有一個自定義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

回答

1

也許是因爲只有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-自定義字段/ –

相關問題