我在“Multisite Media Display”插件中使用的这个函数将把所有子网站放在一个数组中。请注意,根据WP版本的不同,有两种方法可以做到这一点,因为WP\\u get\\u站点从4.6+开始就不推荐使用,但在4.6+中仍然允许使用,即使它不能正确返回站点名称。
function mmd_get_sites_array($atts, $xedit=0) {
// needed since wp_get_sites deprecated as of 4.6+, but can\'t use replacement get_sites in < 4.6
global $wp_version;
// WordPress 4.6
if ( $wp_version >= 4.6 ) { // instead of looking for deprecated functions that might still exist
$subsites_object = get_sites();
$subsites = objectToArray($subsites_object);
foreach( $subsites as $subsite ) {
$subsite_id = $subsite ["blog_id"];
$subsite_name = get_blog_details($subsite_id)->blogname;
$subsite_path = $subsite[path];
$subsite_domain = $subsite[domain];
switch_to_blog( $subsite_id );
// show site id and path
echo "<hr>Site:<strong> $subsite_id - $subsite_name</strong> ; Path: <strong>$subsite_path</strong><hr>";
$xsiteurl = $subsite_domain . $subsite_path;
// do something here; previous creates the url of the site
restore_current_blog();
}
}
if ( $wp_version <= 4.5 ) { // WordPress < 4.6
$sites = wp_get_sites();
// and this is how we loop through blogs with <4.6
foreach ( $sites as $site ) {
switch_to_blog( $site[\'blog_id\'] );
// following echos site id and path
echo "<hr>Site:<strong> $site[blog_id]</strong> ; Path: <strong>$site[path]</strong><hr>";
$xsiteurl = $site[domain] . $site[path];
// do something here, previous creates the URL of the site
restore_current_blog();
}
}
return ; // return empty array due to fail
}
$site数组将包含站点属性。
可能效率更高,或者“分类”,但它符合我的目的。
希望这能帮助你开始。