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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 无法让WordPress显示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__说你应该检查:

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

就那么简单

脚本宝典总结

以上是脚本宝典为你收集整理的php – 无法让WordPress显示Multisite中的所有类别全部内容,希望文章能够帮你解决php – 无法让WordPress显示Multisite中的所有类别所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。