函数文档

get_main_site_id()

💡 云策文档标注

概述

get_main_site_id() 函数用于获取指定网络的主站点 ID。在非多站点环境中,返回当前站点 ID;在多站点环境中,根据网络 ID 返回主站点 ID。

关键要点

  • 参数 $network_id 可选,指定网络 ID,默认为当前网络,若为 null 则使用默认值。
  • 返回值类型为 int,表示主站点 ID,若网络不存在则返回 0。
  • 函数内部逻辑:检查是否为多站点,非多站点时返回 get_current_blog_id();多站点时通过 get_network() 获取网络对象并返回其 site_id 属性。
  • 相关函数包括 get_network()、is_multisite()、get_current_blog_id() 和 is_main_site()。
  • 自 WordPress 4.9.0 版本引入。

代码示例

function get_main_site_id( $network_id = null ) {
	if ( ! is_multisite() ) {
		return get_current_blog_id();
	}

	$network = get_network( $network_id );
	if ( ! $network ) {
		return 0;
	}

	return $network->site_id;
}

📄 原文内容

Gets the main site ID.

Parameters

$network_idintoptional
The ID of the network for which to get the main site.
Defaults to the current network.

Default:null

Return

int The ID of the main site.

Source

function get_main_site_id( $network_id = null ) {
	if ( ! is_multisite() ) {
		return get_current_blog_id();
	}

	$network = get_network( $network_id );
	if ( ! $network ) {
		return 0;
	}

	return $network->site_id;
}

Changelog

Version Description
4.9.0 Introduced.