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

时间:2015-04-23 14:46:20

标签: php wordpress archive custom-taxonomy

我的客户坚持要在一个页面上显示所有类别和帖子。不知道,我知道,但我需要这样做。

无论如何,结构是这样的:

第1级别类别标题>二级类别标题> ...>第n级别类别标题>发布内容。

这将以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>

功能说明:

  • 第一类获得“主要”类,第二类“次要”,以及所有后续“第三类”。如果需要,我可以使用CSS解决这个问题。
  • <h2>用于主要标题,<h3>用于次要标题,<h4>用于所有后续标题。如果需要,我可以使用CSS解决这个问题。
  • 树中最深的类别显示该类别中的所有产品。

我已尝试使用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);
}

1 个答案:

答案 0 :(得分: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);
}