[英]Displaying all categories and posts on one page in a hierarchy
My client insists on having all categories and posts displayed on one page. 我的客户坚持将所有类别和帖子显示在一页上。 Bad idea, I know, but I need to do it.
我知道这是个坏主意,但我需要这样做。
Anyway, the structure is something like this: 无论如何,结构是这样的:
1st Level Category Title > 2nd Level Category Title > ... > nth Level Category Title > Post Content. 一级类别标题>二级类别标题> ...>二级类别标题>帖子内容。
This will be structured in HTML something like: 这将以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>
Explanation of functionality: 功能说明:
<h2>
is used for primary title, <h3>
is used for secondary title, <h4>
is used for all subsequent. <h2>
用于主要标题, <h3>
用于辅助标题, <h4>
用于所有后续标题。 I can work around this with CSS if need be. I've tried several things with get_terms()
and get_categories()
, but I can't figure out how to tell if a category is at its deepest level, and I can't figure out how to go infinitely deep in to a category tree (I end up having to duplicate my code for each new layer). 我已经尝试过使用
get_terms()
和get_categories()
,但是我无法弄清楚如何判断某个类别是否处于最深层次,而且我也无法弄清楚如何无限深入该类别。树(我最终不得不为每个新层重复我的代码)。
I'm current experimenting with this: 我目前正在对此进行试验:
$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;
}
}
}
This does check to see if it's the deepest in a tree, but it doesn't actually construct the tree. 这确实会检查它是否在树中最深,但实际上并没有构造树。 I'll keep toying with it and report back any progress.
我会继续努力,并报告任何进展。 Help would be immensely appreciated.
帮助将不胜感激。
UPDATE 4: With a lot of help from @Nemutaisama, I was able to figure this out! 更新4:在@Nemutaisama的大力帮助下,我得以弄清楚这一点! Here's my final code (slightly modified from their answer below):
这是我的最终代码(从下面的回答中略作修改):
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);
}
i think recursive function will help you. 我认为递归函数将为您提供帮助。 something like this
像这样的东西
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.