wordpress博客多站点获取当前博客信息示例
首先,什么是当前博客?和单独站点不同的是,Multisite会产生多个博客,称之为blog,即子站点(包括主站点)。而你所访问的这个子站点被成为当前博客。当前博客的信息主要是指博客的名称、路径、ID号等博客本身的唯一性信息,而不包含这个博客有多少用户,多少文章,用户和文章可以通过获得当前博客信息之后的ID号再来获得。
获取当前博客信息可以在插件中这样获取:
复制代码
代码如下:
global $current_blog;
global $current_blog;
它的返回结果是:
复制代码
代码如下:
stdClass Object
(
[blog_id] => 3
[site_id] => 1
[domain] => demo.utubon.com
[path] => /neighborhood/
[registered] => 2013-08-01 10:31:03
[last_updated] => 2013-08-01 02:31:03
[public] => 1
[archived] => 0
[mature] => 0
[spam] => 0
[deleted] => 0
[lang_id] => 0
)
stdClass Object
(
[blog_id] => 3
[site_id] => 1
[domain] => demo.utubon.com
[path] => /neighborhood/
[registered] => 2013-08-01 10:31:03
[last_updated] => 2013-08-01 02:31:03
[public] => 1
[archived] => 0
[mature] => 0
[spam] => 0
[deleted] => 0
[lang_id] => 0
)
也可以使用get_blog_details函数:
复制代码
代码如下:
$current_blog = get_blog_details();
$current_blog = get_blog_details();
它的返回结果是:
复制代码
代码如下:
stdClass Object
(
[blog_id] => 3
[site_id] => 1
[domain] => demo.utubon.com
[path] => /neighborhood/
[registered] => 2013-08-01 10:31:03
[last_updated] => 2013-08-01 02:31:03
[public] => 1
[archived] => 0
[mature] => 0
[spam] => 0
[deleted] => 0
[lang_id] => 0
[blogname] => Neighborhood
[siteurl] => <a href="http://demo.utubon.com/neighborhood">http://demo.utubon.com/neighborhood</a>
[post_count] =>
)
// 比global $current_blog的信息更多
stdClass Object
(
[blog_id] => 3
[site_id] => 1
[domain] => demo.utubon.com
[path] => /neighborhood/
[registered] => 2013-08-01 10:31:03
[last_updated] => 2013-08-01 02:31:03
[public] => 1
[archived] => 0
[mature] => 0
[spam] => 0
[deleted] => 0
[lang_id] => 0
[blogname] => Neighborhood
[siteurl] => <a href="http://demo.utubon.com/neighborhood">http://demo.utubon.com/neighborhood</a>
[post_count] =>
)
// 比global $current_blog的信息更多
这基本上就满足了我们的需要,不过还有一个比较绕弯子的方法,使用get_blog_id_from_url函数。如果你的多站点是使用子目录的形式,就使用:
复制代码
代码如下:
$blog_id = get_blog_id_from_url("example.com", "/blog1/");
$blog_id = get_blog_id_from_url("example.com", "/blog1/");
如果使用的是子域名的形式,就使用:
复制代码
代码如下:
$blog_id = get_blog_id_from_url("blog1.example.com");
$blog_id = get_blog_id_from_url("blog1.example.com");
而上面的这些参数,我们可以使用$_SERVER['SERVER_NAME']和$_SERVER["REQUEST_URI"]获取。
获得$blog_id之后各种事情就好办了。