我正在使用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);
为什么不显示类别?
答案 0 :(得分:4)
如同b__所说,你应该检查:
我做过像你这样的事情,这是代码,万一你想尝试一下:
// 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 );
自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 );
简单......
答案 1 :(得分:1)
你有没有尝试过:
<?php wp_list_categories("title_li=");?>