admin管理员组文章数量:1252691
The codex defines it as 'Set up global post data. Helps to format custom query results for using Template tags.' I don't really understand this.
Here is a code example :
global $post;
$args = array( 'numberposts' => -1);
$posts = get_posts($args);
foreach( $posts as $post) : setup_postdata($post);
echo $post->ID;
endforeach;
Please can you explain?
The codex defines it as 'Set up global post data. Helps to format custom query results for using Template tags.' I don't really understand this.
Here is a code example :
global $post;
$args = array( 'numberposts' => -1);
$posts = get_posts($args);
foreach( $posts as $post) : setup_postdata($post);
echo $post->ID;
endforeach;
Please can you explain?
Share Improve this question asked May 15, 2013 at 19:34 user28566user285661 Answer
Reset to default 21Template tag functions rely on global variables to access the post being processed and retrieve data from it or related to it.
The main variable among these is $post
, which holds the post object itself.
In your example it's not explicit, but what is happening is that your loop assigns data to $post
. If its name wasn't $post
, you would need to name it explicitly (global $post; $post = $some_other_post;
).
However, there are a bunch of other globals and the purpose of setup_postdata()
is to fill them with data. If you look at the source, these are:
global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
Note that in most cases you should be calling wp_reset_postdata()
afterwards to return globals to their original state.
本文标签: What does setuppostdata (post ) do
版权声明:本文标题:What does setup_postdata ($post ) do? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740316806a2260465.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论