the_post()
云策文档标注
概述
the_post() 是 WordPress 循环中用于迭代文章索引的核心函数,它设置当前文章为循环中的下一篇文章,以便访问文章数据。
关键要点
- the_post() 在循环中调用,用于推进到下一篇文章并设置全局变量。
- 它依赖于全局 $wp_query 对象,通过 WP_Query::the_post() 方法实现。
- 函数内部检查 $wp_query 是否存在,确保循环已正确初始化。
- 在循环中,the_post() 通常与 have_posts() 和 while 语句结合使用。
- 调用 the_post() 后,可以使用如 the_title()、the_content() 等模板标签访问文章内容。
代码示例
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
// 输出文章标题和内容
the_title();
the_content();
}
}注意事项
- 确保在调用 the_post() 前已使用 have_posts() 检查是否有文章。
- the_post() 会修改全局状态,影响后续模板标签的输出。
- 在自定义查询中,需正确设置 WP_Query 实例以避免冲突。
原文内容
Iterate the post index in the loop.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 3 content
tarau
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.
Skip to note 4 content
Aurovrata Venet
As per the documentation, the available functions to access the post within the loop are:
<a href="https://developer.wordpress.org/reference/functions/next_post_link/">next_post_link()</a>– a link to the post published chronologically after the current post<a href="https://developer.wordpress.org/reference/functions/previous_post_link/">previous_post_link()</a>– a link to the post published chronologically before the current post<a href="https://developer.wordpress.org/reference/functions/the_category/">the_category()</a>– the category or categories associated with the post or page being viewed<a href="https://developer.wordpress.org/reference/functions/the_author/">the_author()</a>– the author of the post or page<a href="https://developer.wordpress.org/reference/functions/the_content/">the_content()</a>– the main content for a post or page<a href="https://developer.wordpress.org/reference/functions/the_excerpt/">the_excerpt()</a>– the first 55 words of a post’s main content followed by an ellipsis (…) or read more link that goes to the full post. You may also use the “Excerpt” field of a post to customize the length of a particular excerpt.<a href="https://developer.wordpress.org/reference/functions/the_id/">the_ID()</a>– the ID for the post or page<a href="https://developer.wordpress.org/reference/functions/the_meta/">the_meta()</a>– the custom fields associated with the post or page<a href="https://developer.wordpress.org/reference/functions/the_shortlink/">the_shortlink()</a>– a link to the page or post using the url of the site and the ID of the post or page<a href="https://developer.wordpress.org/reference/functions/the_tags/">the_tags()</a>– the tag or tags associated with the post<a href="https://developer.wordpress.org/reference/functions/the_title/">the_title()</a>– the title of the post or page<a href="https://developer.wordpress.org/reference/functions/the_time/">the_time()</a>– the time or date for the post or page. This can be customized using standard php date function formatting.including the following conditional tags:
<a href="https://developer.wordpress.org/reference/functions/is_home/">is_home()</a>– Returns true if the current page is the homepage<a href="https://developer.wordpress.org/reference/functions/is_admin/">is_admin()</a>– Returns true if inside Administration Screen, false otherwise<a href="https://developer.wordpress.org/reference/functions/is_single/">is_single()</a>– Returns true if the page is currently displaying a single post<a href="https://developer.wordpress.org/reference/functions/is_page/">is_page()</a>– Returns true if the page is currently displaying a single page<a href="https://developer.wordpress.org/reference/functions/is_page_template/">is_page_template()</a>– Can be used to determine if a page is using a specific template, for example:is_page_template('about-page.php')<a href="https://developer.wordpress.org/reference/functions/is_category/">is_category()</a>– Returns true if page or post has the specified category, for example:is_category('news')<a href="https://developer.wordpress.org/reference/functions/is_tag/">is_tag()</a>– Returns true if a page or post has the specified tag<a href="https://developer.wordpress.org/reference/functions/is_author/">is_author()</a>– Returns true if inside author’s archive page<a href="https://developer.wordpress.org/reference/functions/is_search/">is_search()</a>– Returns true if the current page is a search results page<a href="https://developer.wordpress.org/reference/functions/is_404/">is_404()</a>– Returns true if the current page does not exist<a href="https://developer.wordpress.org/reference/functions/has_excerpt/">has_excerpt()</a>– Returns true if the post or page has an excerpt