【发布时间】:2015-04-23 14:46:20
【问题描述】:
我的客户坚持将所有类别和帖子显示在一个页面上。坏主意,我知道,但我必须这样做。
反正结构是这样的:
1 级分类标题 > 2 级分类标题 > ... > 第 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);
}
【问题讨论】:
标签: php wordpress archive custom-taxonomy