函数文档

get_blog_permalink()

💡 云策文档标注

概述

get_blog_permalink() 函数用于在多站点网络中获取指定博客上某篇文章的固定链接。它通过临时切换博客上下文,调用 get_permalink() 来实现。

关键要点

  • 函数接受两个必需参数:$blog_id(源博客ID)和 $post_id(目标文章ID)。
  • 返回值为字符串,即文章的固定链接。
  • 内部使用 switch_to_blog() 和 restore_current_blog() 来管理博客切换。
  • 适用于 WordPress 多站点环境,自 MU 3.0.0 版本引入。

代码示例

// 从网络中的任何博客,获取博客ID为3上文章ID为6的固定链接。
$permalink = get_blog_permalink( 3, 6 );

📄 原文内容

Gets the permalink for a post on another blog.

Parameters

$blog_idintrequired
ID of the source blog.
$post_idintrequired
ID of the desired post.

Return

string The post’s permalink.

Source

function get_blog_permalink( $blog_id, $post_id ) {
	switch_to_blog( $blog_id );
	$link = get_permalink( $post_id );
	restore_current_blog();

	return $link;
}

Changelog

Version Description
MU (3.0.0) 1.0 Introduced.

User Contributed Notes