admin管理员组文章数量:1334149
I'm a beginner wordpress developer, getting a lot confused in the_post();
function, apparently it doesn't perform anything that can be visualized, but if I do not invoke this function my code breaks up. can someone please explain to me.
Even though I've sufficient knowledge of programming languages, but this thing is getting me confused.
Thanks in advance to helping hands
while (have_posts()) {
the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php }
I'm a beginner wordpress developer, getting a lot confused in the_post();
function, apparently it doesn't perform anything that can be visualized, but if I do not invoke this function my code breaks up. can someone please explain to me.
Even though I've sufficient knowledge of programming languages, but this thing is getting me confused.
Thanks in advance to helping hands
while (have_posts()) {
the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php }
Share
Improve this question
asked Jun 14, 2020 at 4:34
LintLint
1131 silver badge7 bronze badges
1
- 1 This query can be answered easily by looking at the WordPress documentation. In fact WP documentation has a specific topic on this particular function. – CodeMascot Commented Jun 14, 2020 at 8:06
1 Answer
Reset to default 1I'm just going to quote a brief, yet comprehensive, comment on the official reference page for the_post()
:
Function
the_post()
checks whether the loop has started and then sets the current post by moving, each time, to the next post in the queue.
Additionally, the "current post" here refers to the current item in WP_Query::$posts
which is an array of posts returned by the WordPress query.
So for example, if WP_Query::$posts
contains 5 items, the first call to the_post()
will move the array pointer to the first item in that array, i.e. WP_Query::$posts[0]
. Then the next calls move the pointer to WP_Query::$posts[1]
, WP_Query::$posts[2]
and so forth until the last item is reached.
So the_post()
indeed doesn't display or even return anything, but without calling that function, you'd be stuck in an infinite while
execution — for as long as the have_posts()
returns true
(i.e. there is a next item in WP_Query::$posts
), hence as you've seen it, the page breaks.
And one more thing you should know, is that the current post is referenced by the global $post
variable which is relied upon by the_title()
, the_content()
, get_the_title()
and other the_
and get_the_
functions which display or retrieve/use data from the current post.
本文标签: postsWhy this function doesn39t works without thepost
版权声明:本文标题:posts - Why this function doesn't works without the_post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742350882a2458465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论