无法让WordPress在多站点中显示所有类别


Can't get WordPress to display all categories in Multisite

我正在使用WordPress Multisite,我正在尝试在一个页面上显示每个站点中的所有类别。当我在我的管理员帐户上时,以下代码有效。但是,当我切换到任何其他帐户时,不会显示任何类别。

$t=get_current_blog_id();
foreach(function_that_gets_blogs() as $k=>$blog){ 
    switch_to_blog($blog['blog_id']);
    print_r(get_categories(array('hide_empty'=>true))); // prints "array()"
    foreach(get_categories(array('hide_empty'=>true)) as $cat){
         ...
    }
}
switch_to_blog($t);

为什么类别不显示?

就像b__说的那样,你应该检查:

  • 你在哪里使用此代码?(功能.php,插件)
  • 您应该逐个禁用插件,以检查是否有一些插件干扰
  • 在最后一种情况下更改主题

我已经做了一些类似你的事情,这是代码,如果你想尝试一下:

// Current Site
$current = get_current_site();
// All Sites
$blogs = get_blog_list( 0, 'all' );
foreach ( $blogs as $blog ) {
    // switch to the blog
    switch_to_blog( $blog['blog_id'] );
    // get_categories args
    $args = array(
        'hide_empty' => true
    );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
        $link = get_category_link( $category->term_id );
        $name = $category->name;
        printf( '<a href="%s" title="%s">%s</a> ', $link, $name, $name );
    }
}
// return to the current site
switch_to_blog( $current->id );

更新 2014/06/01

自版本 3.0 起,不推荐使用函数get_blog_list();,您应该在 wp_get_sites();

// Current Site
$current = get_current_site();
// All Sites
$blogs = wp_get_sites();
foreach ( $blogs as $blog ) {
    // switch to the blog
    switch_to_blog( $blog['blog_id'] );
    // get_categories args
    $args = array(
        'hide_empty' => true
    );
    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
        $link = get_category_link( $category->term_id );
        $name = $category->name;
        printf( '<a href="%s" title="%s">%s</a> ', $link, $name, $name );
    }
}
// return to the current site
switch_to_blog( $current->id );

就这么简单...

你试过吗:

<?php wp_list_categories("title_li=");?>