get_blog_post()
云策文档标注
概述
get_blog_post() 函数用于从网络中的任意站点获取博客文章,类似于 get_post(),但扩展了跨站点检索能力。
关键要点
- 函数功能:从指定博客站点获取文章,返回 WP_Post 对象或 null。
- 参数说明:$blog_id(必需,博客ID)和 $post_id(必需,文章ID)。
- 内部实现:使用 switch_to_blog() 切换博客,调用 get_post() 获取文章,再 restore_current_blog() 恢复当前博客。
- 相关函数:switch_to_blog()、restore_current_blog() 和 get_post()。
- 版本历史:自 MU (3.0.0) 版本引入。
代码示例
$post_6 = get_blog_post( 3, 6 );
if ( $post_6 ) {
echo $post_6->post_title;
}
原文内容
Gets a blog post from any site on the network.
Description
This function is similar to get_post() , except that it can retrieve a post from any site on the network, not just the current site.
Parameters
$blog_idintrequired-
ID of the blog.
$post_idintrequired-
ID of the post being looked for.
Source
function get_blog_post( $blog_id, $post_id ) {
switch_to_blog( $blog_id );
$post = get_post( $post_id );
restore_current_blog();
return $post;
}
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |
Skip to note 2 content
Codex
Example
Get the post with id 6 from site 3:
$post_6 = get_blog_post( 3, 6 ); if ( $post_6 ) { echo $post_6->post_title; }