get_id_from_blogname()通过博客别名获取博客id(多站点)

目录

描述

Given a blog's (subdomain or directory) name, retrieve it's id.

get_id_from_blogname() 描述

用法

<?php get_id_from_blogname$slug ?>

get_id_from_blogname() 用法

参数

$slug

(string) (必填) The blog's slug

默认值: None

get_id_from_blogname() 参数

返回值

(integer

The blog id

(NULL

If there isn't a blog with the provided slug

get_id_from_blogname() 返回值

示例

Getting the the id of a blog by name ($slug)

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$slug = 'first-site';
$id = get_id_from_blogname($slug);

Switching blogs based on the blogs name ($slug)

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$slug = 'another-site';
$id = get_id_from_blogname($slug);
switch_to_blog($id);

// do great things

restore_current_blog();

get_id_from_blogname() 示例

历史

添加于 版本: 3.0.0

get_id_from_blogname() 历史

源文件

get_id_from_blogname() 函数的代码位于 wp-includes/ms-blogs.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Given a blog's (subdomain or directory) slug, retrieve its id.
 *
 * @since MU
 *
 * @global wpdb $wpdb
 *
 * @param string $slug
 * @return int A blog id
 */
function get_id_from_blogname( $slug ) {
	global $wpdb;

	$current_site = get_current_site();
	$slug = trim( $slug, '/' );

	$blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
	if ( $blog_id )
		return $blog_id;

	if ( is_subdomain_install() ) {
		$domain = $slug . '.' . $current_site->domain;
		$path = $current_site->path;
	} else {
		$domain = $current_site->domain;
		$path = $current_site->path . $slug . '/';
	}

	$blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
	wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
	return $blog_id;
}

get_id_from_blogname() 源文件

相关