據說 wp_list_categories() 函數返回一個字符串。但是,它不會返回任何內容,而是在另一個位置激活類別列表的輸出。頁面內容生成如下function build_index_posts(...){ $html = ''; $html.= '<div class="one">'; $html.= '<h1>'.$header_arr['title'].'</h1>'; $html.= '</div>'; return $html;} 我試圖輸出一個包含子類別列表的塊,但它沒有插入到指定位置,而是插入到頁面的最開頭。 function build_index_posts(...){ $html = ''; $html.= '<div class="one">'; $html.= '<h1>'.$header_arr['title'].'</h1>'; $html.= '</div>'; $category = get_queried_object(); $category_id = $category->term_id; $li_args = array( 'child_of' => $category_id, 'depth' => 1, 'style' => 'none', 'hide_empty' => 0, 'orderby' => 'name', 'show_count' => 0, 'pad_counts' => 0, 'hierarchical' => 1, 'title_li' => '' ); $cat_list = wp_list_categories($li_args); $cat_list = str_replace('<br>', '', $cat_list); $html.= '<div style="display: none">'; $html.= $cat_list; // NULL ?? $html.= '</div>'; return $html; } 如何在適當的塊中插入類別列表?
1 回答

萬千封印
TA貢獻1891條經驗 獲得超3個贊
有兩種使用方法wp_list_categories()- 您使用它的方式將立即輸出代碼中調用它的列表 - 這就是您在頁面頂部看到它的原因。
如果您想獲取類別列表作為變量,則需要傳遞包含echo值為false(默認為 true)的參數:
$li_args = array(
? ? ?'echo' => false,
? ? ? /* the rest of your args */
);
// now you can save the list in a variable.
$cat_list = wp_list_categories($li_args);
- 1 回答
- 0 關注
- 119 瀏覽
添加回答
舉報
0/150
提交
取消