Php Can';t让WordPress在多站点中显示所有类别
我正在使用WordPress Multisite,并试图在一个页面上显示每个站点中的所有类别。当我使用我的管理员帐户时,以下代码起作用。但是,当我切换到任何其他帐户时,不会显示任何类别Php Can';t让WordPress在多站点中显示所有类别,php,wordpress,Php,Wordpress,我正在使用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_
$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);
为什么没有显示类别?您是否尝试过:
<?php wp_list_categories("title_li=");?>
如前所述,您应该检查:
- 你在哪里使用这个代码?(functions.php,plugin)
- 您应该逐个禁用插件,以检查是否有干扰
- 最后一种情况是改变主题
// 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 );
简单地说,…is_admin()函数不检查当前用户是否具有管理权限-它用于检查当前页面是否是管理界面的一部分。在文档中检查您自己-。我希望为每个用户显示类别,而不仅仅是管理员。您的代码对我来说很好。。。疑问:1)您在哪里使用此代码?2) 我知道在MS上很难,但是你能关闭所有非必要的插件并检查它是否持续存在吗?3) 里面有插件吗?嗨@PratikJoshi我刚刚测试了代码,对我来说很好。我能为您做些什么吗?对不起,我必须从multisite的第一个博客中获取文章/类别。所以作为一名程序员,我通常认为博客的索引从0开始,但实际上第一个博客的索引是1,所以我将其改为1,并且成功了,谢谢!)
// 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 );