2014-02-13 27 views
0

我在WordPress循環中使用WP_Query,出於某種原因,我無法使「其他」部分工作。基本上我在尋找類別59的帖子。如果沒有我想顯示一些文字。當類別中的任何帖子出現時,循環都可以正常工作,但如果沒有,則什麼也不顯示。似乎無法弄清楚爲什麼這不起作用。這是我的代碼。任何幫助深表感謝!其他部分的WordPress循環無法正常工作

<?php 

//The Query 

$custom_posts = new WP_Query(); 
$custom_posts->query('cat=59'); 

//The Loop 
if (have_posts()) : while ($custom_posts->have_posts()) : $custom_posts->the_post(); 

?> 

<article> 
<div class="thenews"> 
    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Go to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1> 
    <h2>Posted on: <?php the_time('F jS, Y') ?></h2> 
    <?php the_excerpt(); ?> 
</div><!-- thenews div ENDS here --> 
<div class="clearfloats"><!-- clears the elements above --></div> 
</article> 

<?php endwhile; else: ?> 

<article> 
<div class="thenews"> 
    <p>Nothing here to see.</p> 
</div><!-- thenews div ENDS here --> 
<div class="clearfloats"><!-- clears the elements above --></div> 
</article> 

<? endif; 

//Reset Query 
wp_reset_query(); 

?> 

回答

3

你沒有使用自定義循環的has_posts方法,這是爲什麼其他沒有觸發。

變化:

//The Loop 
if (have_posts()) : while ($custom_posts->have_posts()) : $custom_posts->the_post(); 

要:

//The Loop 
if ($custom_posts->have_posts()) : while ($custom_posts->have_posts()) : $custom_posts->the_post(); 
+0

謝謝!對不起,我花了一段時間回到你身邊,我期待自動通知任何回覆,並且從未發生過。再次感謝。 – user3305765