2016-09-15 107 views
0

我在Wordpress模板文件中只有以下代碼,儘管數據庫中只有兩個帖子,但它會進入無限循環。Wordpress查詢進入無限循環

$args = array(
    'posts_per_page' => '‐1', 
    'post_type' => 'products', 
); 

$myProducts = new WP_Query($args); 

// The Loop 
while ($myProducts->have_posts()) : $myProducts‐>the_post(); 
    echo 'loop body'; 
endwhile; 

// Reset Post Data 
wp_reset_postdata(); 

如果我打印$ myProducts變量,我可以在那裏看到兩個帖子。爲什麼無限循環呢?

+0

在這裏沒有包含WordPress模板文件中的其他代碼嗎?你是積極的,無限循環發生在你認爲的地方嗎? –

+0

是的,這是我的模板中唯一的代碼。我知道無限循環處於while狀態,因爲我的屏幕充滿了「循環體」。 –

回答

0
<?php 
$params = array(
     'posts_per_page' => 5, 
     'post_type' => 'product' 
); 
$wc_query = new WP_Query($params); 
?> 
<?php if ($wc_query->have_posts()) : ?> 
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?> 
<?php the_title(); ?> 
<?php endwhile; ?> 
<?php wp_reset_postdata(); ?> 
<?php else: ?> 
<p> 
    <?php _e('No Products'); ?> 
</p> 
<?php endif; ?> 

這對我很好。