在层次结构的一页上显示所有类别和帖子

Displaying all categories and posts on one page in a hierarchy

我的客户坚持将所有类别和帖子显示在一个页面上。我知道这是个坏主意,但我必须这样做。

总之,结构是这样的:

一级类别标题 > 二级类别标题 > ... > n 级类别标题 > Post 内容。

这将采用 HTML 的结构,类似于:

<div class="cat primary">
    <h2>1st Level Category Title</h2>
    <div class="cat secondary">
        <h3>2nd Level Category Title</h3>
        <div class="cat tertiary">
            <h4>3rd Level Category Title</h4>
            ...
                <div class="cat tertairy">
                    <h4>nth Level Category Title</h4>
                    <div class="product">
                        <p>Post Content</p>
                    </div>
                </div>
            ...
        </div>
    </div>
    <div class="cat secondary">
        <h3>2nd Level Category Title</h3>
        <div class="product">
            <p>Post Content</p>
        </div>
    </div>
</div>

功能说明:

我已经用 get_terms()get_categories() 尝试了几件事,但我不知道如何判断一个类别是否处于最深层次,我也想不通如何无限深入到类别树(我最终不得不为每个新层复制我的代码)。

我目前正在试验这个:

$categories = get_terms("product-category"); 
if ($categories && !is_wp_error($categories)) {
    foreach($categories as $category) {
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        if (count($children) == 0) {
            echo $category->name;
        }
    }
}

这确实会检查它是否在树的最深处,但实际上并没有构建树。我会继续玩弄它并报告任何进展。将不胜感激。


更新 4: 在@Nemutaisama 的大力帮助下,我能够解决这个问题!这是我的最终代码(根据下面的答案略作修改):

function loadCategories($categories, $level) {
    foreach($categories as $category) {
        $cat_class = "";
        $heading_tag = "";
        if ($level == 1) {
            $cat_class = "primary";
            $heading_tag = "h2 style='text-align:center;'";
        } elseif ($level == 2) {
            $cat_class = "secondary";
            $heading_tag = "h3";
        } else {
            $cat_class = "tertiary";
            $heading_tag = "h4";
        }
        echo "<section class='cat $cat_class'>";
        echo "<header>";
        echo "<$heading_tag>{$category->name}<button>Expand</button></$heading_tag>";
        if ($level == 1) {
            echo "<hr  class='short' />";
        }
        echo "</header>";
        if ($level > 1) {
            echo "<div class='expander'>";
        }
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        if (count($children) == 0) {
            $posts = get_posts(array(
                "post_type" => "products",
                "tax_query" => array(
                    array(
                        "field"    => "term_id",
                        "taxonomy" => "product-category",
                        "terms"    => $category->term_id,
                )),
            ));
            foreach ($posts as $post) {
                if ($level < 2) {
                    $cat_class = "secondary";
                    $heading_tag = "h3";
                } else {
                    $cat_class = "tertiary";
                    $heading_tag = "h4";
                }
                echo "<section class='cat $cat_class'>";
                echo "<header><$heading_tag>{$post->post_title}<button>Expand</button></$heading_tag></header>";
                echo "<div class='expander'>";
                echo "<article>";
                if (get_field("product_number", $post->ID)) {
                    echo "<div class='productNumber'><p># " . get_field("product_number", $post->ID) . "</p></div>";
                }
                echo "<div class='content'>";
                echo wpautop($post->post_content);
                echo "</div><!--/.content-->";
                echo "</article>";
                echo "</div><!--/.expander-->";
                echo "</section><!--/.cat.$cat_class-->";
            }
        }
        loadCategories($children, $level+1);
        if ($level > 1) {
            echo "</div><!--/.expander-->";
        }
        echo "</section><!--/.cat.$cat_class-->";
    }
}
$categories = get_terms("product-category", array(
    "parent" => 0,
));
if ($categories && !is_wp_error($categories)) {
    loadCategories($categories, 1);
}

我认为递归函数会对您有所帮助。 像这样

function loadCategories($categories, $level) {
    foreach($categories as $category) {
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        $cat_class = "";
        $heading_tag = "";
        if ($level == 1) {
            $cat_class = "primary";
            $heading_tag = "h2 style='text-align:center;'";
        } elseif ($level == 2) {
            $cat_class = "secondary";
            $heading_tag = "h3";
        } else {
            $cat_class = "tertiary";
            $heading_tag = "h4";
        }
        echo "<section class='cat $cat_class'>";
        echo "<header>";
        echo "<$heading_tag>{$category->name}<button>Expand</button></$heading_tag>";
        if ($level == 1) {
            echo "<hr  class='short' />";
        }
        echo "</header>";
        if ($level > 1) {
            echo "<div class='expander'>";
        }
        if (count($children) == 0) {
            $posts = get_posts(array(
                "post_type" => "products",
                "tax_query" => array(
                    array(
                        "field"    => "term_id",
                        "taxonomy" => "product-category",
                        "terms"    => $category->term_id,
                )),
            ));
            foreach ($posts as $post) {
                if ($level < 2) {
                    $cat_class = "secondary";
                    $heading_tag = "h3";
                } else {
                    $cat_class = "tertiary";
                    $heading_tag = "h4";
                }
                echo "<section class='cat $cat_class'>";
                echo "<header><$heading_tag>{$post->post_title}<button>Expand</button></$heading_tag></header>";
                echo "<div class='expander'>";
                echo "<article>";
                if (get_field("product_number", $post->ID)) {
                    echo "<div class='productNumber'><p># " . get_field("product_number", $post->ID) . "</p></div>";
                }
                echo "<div class='content'>";
                echo wpautop($post->post_content);
                echo "</div><!--/.content-->";
                echo "</article>";
                echo "</div><!--/.expander-->";
                echo "</section><!--/.cat.$cat_class-->";
            }
        }
        loadCategories($children, $level+1);
        if ($level > 1) {
            echo "</div><!--/.expander-->";
        }
        echo "</section><!--/.cat.$cat_class-->";
    }
}
$categories = get_terms("product-category");
if ($categories && !is_wp_error($categories)) {
    loadCategories($categories, 1);
}