WPMU Functions/switch to blog

来自WordPress中文文档
Zhuozuran讨论 | 贡献2016年5月6日 (五) 06:12的版本 (1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

Note: If you're using WordPress Multisite, you can instead use switch_to_blog()

Description

Switches the active blog until the user calls the restore_current_blog() function. This function is useful if you need to pull posts, or other information, from other blogs, you can then switch back after using restore_current_blog(). Using this function will not load plugins that only run on the blog you switch to.

This switch is to be used for internal and admin area functions. It's too expensive a query to run on the front end.

Parameters

tt$new_blog/tt
(integer) (required) The id of the blog you want to switch to.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

(boolean) 
True on success, false if new_blog was blank, or if you tried to switch to the blog you're on.
(global) 
Sets up a global variable called: switched.

Usage

%%%<?php switch_to_blog($new_blog) ?>%%%

Examples

%%%<?php

   /* 
      for global variables, since it is being changed or updated from time to time,
      please refer to Related Resources for more information
   */
   global $switched;
   switch_to_blog(7);
   echo 'You switched from blog ' . $switched . ' to 7';
   restore_current_blog();
   echo 'You switched back.';

?>%%%

Source File

switch_to_blog() is located in onlyincludecode/wp-includes/ms-blogs.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related Functions

To easily revert back to the blog you were on, restore_current_blog()

Related Resources

Please refer the following links for related variables.WPMU_Global_Variables