2014-02-13 2 views
0

Я использую WP_Query в цикле WordPress, и по какой-то причине я не могу получить часть «else» для работы. В основном я ищу сообщения в категории 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(); 

To:

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

Спасибо! Извините, мне потребовалось некоторое время, чтобы вернуться к вам, я ожидал, что вас автоматически уведомят о любых ответах, и этого никогда не было. Еще раз спасибо. – user3305765

Смежные вопросы