![](/img/trans.png)
[英]How can I display a list of categories and all their posts in WordPress?
[英]Can't get WordPress to display all categories in 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__說你應該檢查:
我做過像你這樣的事情,這是代碼,萬一你想嘗試一下:
// 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 );
函數get_blog_list();
自3.0版以來不推薦使用,因此您應該在wp_get_sites();
更改該函數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 );
就那么簡單...
你試過了嗎:
<?php wp_list_categories("title_li=");?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.