0

When no posts exist I'm used to seeing the message after the else, and I'm not sure why it's not showing it now?

Code:

<?php
     $args = array( 'post_type' => 'event', 'posts_per_page' => 1, 'post_status' => 'future', 'order' => 'ASC' );
     $loop = new WP_Query( $args );
     if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
  ?>
  <div class="event_preview_title"><?php the_title(); ?></div>
  <div class="event_details">
    <div class="event_date"><?php the_time('m/d/y'); ?></div>
    <div class="event_time"><?php the_time('g:i A'); ?></div>
  </div>
  <?php the_post_thumbnail( array(65,65) ); ?>
  <a class="clickthrough" href="<?php bloginfo('wpurl'); ?>/events"></a>
  <?php endwhile; else: ?>
        <p>Bluebird Books may be coming soon to a neighborhood near you!<br />
We are currently on hiatus planning our next season's schedule. New tour dates will be posted to this page once confirmed. Meanwhile, inquiries about appearances and programs are welcomed! If you are interested in having Bluebird visit your business, school, or special event, please contact us.</p>
  <?php endif; ?>

Thanks!

4
  • perhaps there are posts now. have you recently changed the code? Oct 12, 2012 at 21:19
  • 1
    try to use a standard if else statement instead of shorthand..
    – kingkode
    Oct 12, 2012 at 21:20
  • @asad no posts, deleted them all just to be sure. Have not changed the code.. works on other sites I've done, not sure why not on this one..
    – Reuben
    Oct 12, 2012 at 21:32
  • @McMastermind 1 answer below showed that, tried it, commented that I like that code better, however it didn't fix the problem :/
    – Reuben
    Oct 12, 2012 at 21:33

2 Answers 2

4

You made a special WP_Query, but are checking for the wrong posts

if ( have_posts() )

Should be

if ( $loop->have_posts() )
0
2

I'm not exactly sure what happens when you use the weird if/while syntax, but:

endwhile;

I think your endwhile is ending the while loop, and the ; is ending the if statement. I suggest using the standard { and } syntax so this sort of thing will be easier to read:

<?php
    $args = array( 'post_type' => 'event', 'posts_per_page' => 1, 'post_status' => 'future', 'order' => 'ASC' );
    $loop = new WP_Query( $args );
    if (have_posts()) {
        while ( $loop->have_posts() ) {
            $loop->the_post();
?>
  <div class="event_preview_title"><?php the_title(); ?></div>
  <div class="event_details">
    <div class="event_date"><?php the_time('m/d/y'); ?></div>
    <div class="event_time"><?php the_time('g:i A'); ?></div>
  </div>
<?php
            the_post_thumbnail( array(65,65) );
?>
  <a class="clickthrough" href="<?php bloginfo('wpurl'); ?>/events"></a>
<?php
        }
    } else {
?>
        <p>Bluebird Books may be coming soon to a neighborhood near you!<br />
We are currently on hiatus planning our next season's schedule. New tour dates will be posted to this page once confirmed. Meanwhile, inquiries about appearances and programs are welcomed! If you are interested in having Bluebird visit your business, school, or special event, please contact us.</p>
<?php
    }
?>
2
  • I like this format way better! It makes more sense for sure. However, it didn't fix the problem :/
    – Reuben
    Oct 12, 2012 at 21:31
  • @stephen gave the fix, however I really appreciate this code and will be using it forever!
    – Reuben
    Oct 12, 2012 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.