WordPress 类别和子类别

发布于 2024-09-15 21:20:11 字数 275 浏览 1 评论 0原文

我正在对我的博客主题进行一些更改,当用户位于父类别内时,我需要列出子类别,例如:

  • main-category -- 子目录1 -- subcat2

因此,仅当用户位于主类别内时,我才需要显示 subcat1 和 subcat2。目前我有 2 个 div,一个用于主类别(这是我需要用户在所有页面上看到的内容),另一个 div 在主类别 div 下方显示子类别。

这可能吗?我对我的英语感到抱歉,如果这太混乱了,请告诉我。

感谢您抽出时间。 问候

I am doing some changes on my blog theme, and i need to list the subcategories when user is inside the parent category, example:

  • main-category
    -- subcat1
    -- subcat2

So i need to show the subcat1 and subcat2 only when the user is inside main-category. At this moment i have 2 divs, one for the main-categories (this is what i need users to see on all pages) and one div bellow the main-category div to show the subcategories.

Is this possible? I am sorry for my English, let me know if this is too confuse.

Thanks for your time.
Regards

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

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

发布评论

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

评论(2

大海や 2024-09-22 21:20:11

是的,可以使用 get_the_category()wp_list_categories()child_of 参数。在您的主类别页面上使用类似以下内容:

$category = get_the_category();
wp_list_categories('child_of=' . $category[0]->cat_ID);

Yes, it's possible using get_the_category() and wp_list_categories() with the child_of parameter. Use something like the following on your main-category page:

$category = get_the_category();
wp_list_categories('child_of=' . $category[0]->cat_ID);
夏日落 2024-09-22 21:20:11

尝试使用函数

$categories = get_categories($args);
$html       = '';
foreach($categories as $cat){
if($cat->category_parent == 0){
$html .= '';<--- your code
$childCategories = get_categories('child_of='.$cat->cat_ID.'');
if(!empty($childCategories)){
foreach($childCategories as $ccat){
$html .= '';<-- your code'

}}} $html .='';
return $html;}

为了获取更多的孩子,只需添加新变量并获取她的 child_of 的类别

try with a function <?php echo list_categories(); ?>

$categories = get_categories($args);
$html       = '';
foreach($categories as $cat){
if($cat->category_parent == 0){
$html .= '';<--- your code
$childCategories = get_categories('child_of='.$cat->cat_ID.'');
if(!empty($childCategories)){
foreach($childCategories as $ccat){
$html .= '';<-- your code'

}}} $html .='';
return $html;}

for get more childrens only add the new variable and get the categories of her child_of

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