钩子文档

the_post

💡 云策文档标注

概述

the_post 是一个 WordPress 动作钩子,在文章数据设置完成后触发,允许开发者修改文章对象。文章对象和查询对象通过引用传递,无需返回值。

关键要点

  • the_post 钩子在文章数据设置后立即触发,用于修改文章对象
  • 参数包括 $post(WP_Post 对象,引用传递)和 $query(WP_Query 对象,引用传递)
  • 无需返回值,因为对象通过引用传递
  • 从 WordPress 2.8.0 版本引入,4.1.0 版本添加了 $query 参数

代码示例

function my_the_post_action( $post_object ) {
    // modify post object here
}
add_action( 'the_post', 'my_the_post_action' );

注意事项

  • 该钩子常用于在文章显示前自定义文章数据,如修改标题或内容
  • 与 WP_Query::setup_postdata() 方法相关,用于设置全局文章数据

📄 原文内容

Fires once the post data has been set up.

Parameters

$postWP_Post
The Post object (passed by reference).
$queryWP_Query
The current Query object (passed by reference).

More Information

The ‘the_post’ action hook allows developers to modify the post object immediately after being queried and setup.

The post object is passed to this hook by reference so there is no need to return a value.

Example

function my_the_post_action( $post_object ) {
// modify post object here
}
add_action( 'the_post', 'my_the_post_action' );

Source

do_action_ref_array( 'the_post', array( &$post, &$this ) );

Changelog

Version Description
4.1.0 Introduced $query parameter.
2.8.0 Introduced.