4

我正在使用 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);

为什么不显示类别?

4

2 回答 2

4

就像b__说你应该检查:

  • 您在哪里使用此代码?(functions.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

该功能get_blog_list();自 3.0 版以来已弃用,您应该在其中更改该功能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 );

就那么简单...

于 2013-11-04T23:07:33.413 回答
1

你试过了吗:

<?php wp_list_categories("title_li=");?>
于 2013-11-04T14:28:20.580 回答