1
A
回答
2
你或許可以做到這一點與Walker,但我試了老式的方法。
$categories = get_categories();
// First index all categories by parent id, for easy lookup later
$cats_by_parent = array();
foreach ($categories as $cat) {
$parent_id = $cat->category_parent;
if (!array_key_exists($parent_id, $cats_by_parent)) {
$cats_by_parent[$parent_id] = array();
}
$cats_by_parent[$parent_id][] = $cat;
}
// Then build a hierarchical tree
$cat_tree = array();
function add_cats_to_bag(&$child_bag, &$children)
{
global $cats_by_parent;
foreach ($children as $child_cat) {
$child_id = $child_cat->cat_ID;
if (array_key_exists($child_id, $cats_by_parent)) {
$child_cat->children = array();
add_cats_to_bag($child_cat->children, $cats_by_parent[$child_id]);
}
$child_bag[$child_id] = $child_cat;
}
}
add_cats_to_bag($cat_tree, $cats_by_parent[0]);
// With this real tree, this recursive function can check for the cats you need
function has_children_with_posts(&$children)
{
$has_child_with_posts = false;
foreach ($children as $child_cat) {
$has_grandchildren_with_posts = false;
if (isset($child_cat->children)) {
// Here is our recursive call so we don't miss any subcats
if (has_children_with_posts($child_cat->children)) {
$has_grandchildren_with_posts = true;
}
}
if (0 < intval($child_cat->category_count)) {
$has_child_with_posts = true;
} else if ($has_grandchildren_with_posts) {
// This is a category that has no posts, but does have children that do
$child_cat->is_empty_with_children = true;
var_dump($child_cat->name);
}
}
return $has_child_with_posts;
}
has_children_with_posts($cat_tree);
相關問題
- 1. WordPress的wp_list_categories
- 2. WordPress wp_list_categories()格式化
- 3. WordPress的計數wp_list_categories
- 4. WordPress的側邊欄問題與wp_list_categories顯示NO類別
- 5. 有條件使用wp_list_categories(Wordpress)
- 6. WordPress;代碼自定義(wp_list_categories)
- 7. 從wp_list_categories
- 8. WordPress的WordPress問題
- 9. wp_list_categories模擬偏移量
- 10. 添加類wp_list_categories
- 11. wp_list_categories添加類
- 12. WordPress標題問題
- 13. WordPress主題問題
- 14. wordpress add_filter問題
- 15. Wordpress API問題
- 16. Wordpress Loop問題
- 17. Wordpress query_posts問題
- 18. Wordpress parse_query問題
- 19. wordpress onClick問題
- 20. WordPress的問題
- 21. WordPress問題XMLRPC
- 22. WordPress的問題
- 23. Wordpress/Buddypress問題
- 24. Wordpress 404問題
- 25. Wordpress get_the_post_thumbnail();問題
- 26. Wordpress seo問題
- 27. Wordpress stripslashes問題
- 28. wordpress htaccess問題
- 29. wordpress query_posts問題
- 30. Wordpress wp_redirect問題