is_main_site()
云策文档标注
概述
is_main_site() 函数用于判断指定站点是否为当前网络的主站点。在非多站点环境下,该函数始终返回 true。
关键要点
- 函数接受两个可选参数:$site_id(站点ID,默认为当前站点)和 $network_id(网络ID,默认为当前网络)。
- 返回值类型为布尔值,true 表示 $site_id 是网络的主站点,或在非多站点环境下。
- 该函数替代了已弃用的 is_main_blog() 函数(自 3.0.0 版本起)。
- 内部逻辑:首先检查是否启用多站点,若否则返回 true;然后获取或转换 $site_id,并与 get_main_site_id($network_id) 进行比较。
代码示例
if ( is_main_site( $blog_id ) ) {
// display something special for the main site.
}
原文内容
Determines whether a site is the main site of the current network.
Parameters
$site_idintoptional-
Site ID to test. Defaults to current site.
Default:
null $network_idintoptional-
Network ID of the network to check for.
Defaults to current network.Default:
null
Source
function is_main_site( $site_id = null, $network_id = null ) {
if ( ! is_multisite() ) {
return true;
}
if ( ! $site_id ) {
$site_id = get_current_blog_id();
}
$site_id = (int) $site_id;
return get_main_site_id( $network_id ) === $site_id;
}
Skip to note 2 content
Codex
Example
if ( is_main_site( $blog_id ) ) { // display something special for the main site. }