Reputation: 21
I'm using a WP_Query in a WordPress loop and for some reason I can not get the "else" portion to work. Basically I'm looking for posts in the category of 59. If there are none I want to display some text. The loop works fine when any posts in the category are present, but if there are none, nothing shows up. Can't seem to figure out why this isn't working. Here's my code. Any help is much appreciated!
<?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();
?>
Upvotes: 0
Views: 45
Reputation: 19308
You're not using the have_posts method of your custom loop which is why else isn't firing.
Change:
//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();
Upvotes: 3