2015-01-14 63 views
1

我有這段代碼的工作原理,但它只顯示5個職位(與自定義分類)應該是7.如果我點擊標籤網址,它顯示7個註冊表,所以它在這個代碼和它的查詢中存在問題。我已經嘗試過nopaging = true,posts_per_page = -1 numberposts = -1並將最後兩個設置爲固定數字,例如7或999;但這些選擇似乎都沒有辦法。WP get_posts showinn only 5 results。 (nopaging,posts_per_page和numberposts不工作)

希望有人能幫助我。謝謝。

add_shortcode('gpp_prod', function($atts, $content = null){ 
ob_start(); 

$atts = shortcode_atts(
    array(
     'category' => '0' 
    ), $atts); 

extract($atts); 

$args = array(
    'post_type'=>'gpp_prod', 
    'orderby' => 'title', 
    'order' => 'ASC' 
); 

if($category > 0){ 
    $args['tax_query'] = array(
     array(
      'posts_per_page' => -1, 
      'taxonomy' => 'cat_prod', 
      'field' => 'term_id', 
      'nopaging' => true, 
      'terms' => $category 
     ) 
    ); 
} 

?> 
<?php $products = get_posts($args); 
$termt = get_term($category, 'cat_prod'); 

<div class="prod-cats"> 
    <h1><?php echo $termt->name;?></h1> 
    <?php if(count($products)>0) { 
     foreach ($products as $key => $value) { ?> 
     <div class="prod-group"> 
      <div class="prod-title"><?php echo do_shortcode($value->post_title); ?></div> 
      <div class="prod-low"> 
       <div class="prod-image"><img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($value->ID)); ?>"/></div> 
       <div role="tabpanel" class="prod-info"> 
        <!-- Nav tabs --> 
        <ul class="nav nav-tabs" role="tablist"> 
         <li role="presentation" class="active"><a href="#tab-1-<?php echo $value->ID?>" aria-controls="home" role="tab" data-toggle="tab">Desc</a></li> 
         <li role="presentation"><a href="#tab-2-<?php echo $value->ID?>" aria-controls="profile" role="tab" data-toggle="tab">Specs</a></li> 
        </ul> 

        <!-- Tab panes --> 
        <div class="tab-content"> 
         <div role="tabpanel" class="tab-pane active" id="tab-1-<?php echo $value->ID?>"><?php echo do_shortcode($value->post_content); ?></div> 
         <div role="tabpanel" class="tab-pane" id="tab-2-<?php echo $value->ID?>"><?php $meta_value = get_post_meta($value->ID, 'meta-text', false); 
               if(!empty($meta_value)) { 
                echo $meta_value[0]; 
               } else { 
                echo 'No specs available'; 
               } 
          ?> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
     <?php } ?> 
    <?php 
    } 
    ?> 
</div> 
<?php 
wp_reset_postdata(); 
return ob_get_clean(); 

});

回答

0

posts_per_page需要進入$ args數組,而不是$ args ['tax_query']。當設置posts_per_page = -1時,nopaging也是多餘的。試試這個:

$args = array(
    'post_type'=>'gpp_prod', 
    'orderby' => 'title', 
    'order' => 'ASC' 
    'posts_per_page' => -1, 
); 

if($category > 0){ 
    $args['tax_query'] = array(
     array(
      'taxonomy' => 'cat_prod', 
      'field' => 'term_id', 
      'terms' => $category 
     ) 
    ); 
} 
+0

這使它工作。謝謝! –