him*_*dri 9 wordpress wordpress-theming
在WordPress主题开发中,我们可以single.php用来显示具体的单个帖子.
为此目的,通常的做法是:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
为什么我需要循环来显示单个帖子?任何人都可以给出一些正当理由吗?
这是使我丧命多年的东西,但我找到了答案:不,您不需要在单个页面中使用LOOP,只需调用the_post()即可获得所有数据
.... BUT ....
Run Code Online (Sandbox Code Playgroud)
如果您不使用循环(while(have_posts())。因此,出于安全原因,您应该使用循环。
另外,人们问我是否需要在循环之前检查是否存在:if(have_posts())?
<?
if( have_posts() ):
while( have_posts() ):
the_post();
.....
endwhile;
endif
?>
Run Code Online (Sandbox Code Playgroud)
不,您不需要检查
.... BUT ....
Run Code Online (Sandbox Code Playgroud)
通过检查,您可以在循环之前包含标题/标题,如果循环为空,则不包含它们。
WordPress循环实例化一些函数,例如the_title()、the_content()和其他函数。
换句话说,您的帖子会在该循环中加载,如果您在单个帖子上,则循环会执行一次。虽然循环可能很奇怪,但它实际上非常有用。
WordPress 使用模板层次结构,这是一种选择为给定帖子/页面加载哪个模板的方法。在你的 中single.php,循环将运行一次。但如果您没有single.php文件,WordPress 将使用index.php该文件来代替同一篇文章。
为了保持一致性,拥有一个适用于任意数量帖子的循环是有帮助的。否则,在您看来index.php,您将需要一个帖子的案例和多个帖子的另一个案例,并且保持一致的模板方法将很困难。
| 归档时间: |
|
| 查看次数: |
4439 次 |
| 最近记录: |