有一个函数get_blog_list()
由于如果网络有多个站点,则可能会消耗大量资源,因此目前不推荐使用该方法。查询wp_blogs
使用自定义查询的WP数据库中的表。
将以下代码添加到主题的函数中。php文件,你应该很好去。
/**
* Build a list of all websites in a network
*/
function wp_list_sites( $expires = 7200 ) {
if( !is_multisite() ) return false;
// Because the get_blog_list() function is currently flagged as deprecated
// due to the potential for high consumption of resources, we\'ll use
// $wpdb to roll out our own SQL query instead. Because the query can be
// memory-intensive, we\'ll store the results using the Transients API
if ( false === ( $site_list = get_transient( \'multisite_site_list\' ) ) ) {
global $wpdb;
$site_list = $wpdb->get_results( $wpdb->prepare(\'SELECT * FROM wp_blogs ORDER BY blog_id\') );
// Set the Transient cache to expire every two hours
set_site_transient( \'multisite_site_list\', $site_list, $expires );
}
$current_site_url = get_site_url( get_current_blog_id() );
$html = \'\' . "n";
foreach ( $site_list as $site ) {
switch_to_blog( $site->blog_id );
$class = ( home_url() == $current_site_url ) ? \' class="current-site-item"\' : \'\';
$html .= "t" . \'blog_id . \'"\' . $class . \'>\' . get_bloginfo(\'name\') . \'\' . "n";
restore_current_blog();
}
$html .= \'\' . "nn";
return $html;
}