简体   繁体   English

没有帖子时,Wordpress循环不显示“ else”

[英]Wordpress loop not showing 'else' when no posts exist

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! 谢谢!

You made a special WP_Query , but are checking for the wrong posts 您做了一个特殊的WP_Query ,但是正在检查错误的帖子

if ( have_posts() )

Should be 应该

if ( $loop->have_posts() )

I'm not exactly sure what happens when you use the weird if/while syntax, but: 我不确定当您使用怪异的if / while语法时会发生什么,但是:

endwhile;

I think your endwhile is ending the while loop, and the ; 认为您的endwhile正在结束while循环,以及; is ending the if statement. 正在结束if语句。 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
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM