为什么像single.php这样的WordPress模板会在运行时循环(have_posts())?

为什么像single.php这样的WordPress模板会在运行时循环(have_posts())?,wordpress,Wordpress,在214主题和许多其他由专家编写的主题中,single.php包含以下代码: while ( have_posts() ) : the_post(); get_template_part( 'content', 'page' ); endwhile; 这有什么不同或更好吗 the_post(); get_template_part( 'content', 'page' ); WordPress是否希望single.php能够显示多于或少于一篇文章,或者作者选择使用while循环的另一个原因是

在214主题和许多其他由专家编写的主题中,single.php包含以下代码:

while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
endwhile;
这有什么不同或更好吗

the_post();
get_template_part( 'content', 'page' );
WordPress是否希望single.php能够显示多于或少于一篇文章,或者作者选择使用while循环的另一个原因是什么?

while(have_posts()):the_post()是存在的,因此您可以像下面的示例代码中那样放置条件逻辑,并且不会得到404,只是在没有post或permalink更改的情况下

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

希望能有帮助