wordpress wp_list_categories 问题

发布于 2024-09-10 17:30:31 字数 81 浏览 4 评论 0原文

我需要制作类似 wp_list_categories 的东西,它显示空类别,但前提是它们的子类别中包含帖子。有人有什么想法吗?

谢谢

I need to make something like wp_list_categories that shows categories that are empty but only if they have children categories that have posts in them. Anyone have any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-09-17 17:30:31

您可能可以使用 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);

You can probably do this with a Walker, but I tried it the old-fashioned way.

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文