加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 无法让WordPress显示Multisite中的所有类别

发布时间:2020-12-13 17:25:45 所属栏目:PHP教程 来源:网络整理
导读:我正在使用WordPress Multisite,我试图在一个页面上显示每个站点中的所有类别.当我在我的管理员帐户上时,以下代码有效.但是,当我切换到任何其他帐户时,不会显示任何类别. $t=get_current_blog_id();foreach(function_that_gets_blogs() as $k=$blog){ switch
我正在使用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__说你应该检查:

>你在哪里使用这段代码? (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

自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> ',$name );
    }

}

// return to the current site
switch_to_blog( $current->id );

就那么简单…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读