簡體   English   中英

無法讓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__說你應該檢查:

  • 你在哪里使用這段代碼? (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

函數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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM