“WPMU Functions/switch to blog”的版本间的差异

来自WordPress中文文档
跳转至: 导航搜索
(1个修订)
(1个版本)
 
(未显示5个用户的5个中间版本)
第1行: 第1行:
To pull posts from another blog id on a Wordpress MU site, put switch_to_blog($id); before your loop where $id is the blog ID of the blog from which you want to query posts.
+
Note: If you're using WordPress Multisite, you can instead use [[Function Reference/switch_to_blog|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 <strong>not</strong> load plugins that only run on the blog you switch to.
  
At the end of the loop, put restore_current_blog();
+
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 ==
 +
 
 +
{{Parameter|$new_blog|integer|The id of the blog you want to switch to.}}
 +
 
 +
== Return Values ==
 +
; <tt>(boolean)</tt> : True on success, false if new_blog was blank, or if you tried to switch to the blog you're on.
 +
; <tt>(global)</tt> : 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 ==
 +
 
 +
<tt>switch_to_blog()</tt> is located in {{Trac|/wp-includes/ms-blogs.php}}.
 +
 
 +
== Related Functions ==
 +
 
 +
To easily revert back to the blog you were on, [[WPMU Functions/restore_current_blog|restore_current_blog()]]
 +
 
 +
== Related Resources ==
 +
 
 +
Please refer the following links for related variables.[[WPMU_Global_Variables|WPMU_Global_Variables]]
 +
 
 +
----
 +
 
 +
[[Category:WPMU]]
 +
[[Category:WPMU Functions]]

2016年5月6日 (五) 06:12的最新版本

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