简体   繁体   English

Wordpress中的类别和子类别顺序问题

[英]category & sub category order issues in wordpress

I have Two parent category on my website, and each of those category have children category, like this : 我的网站上有两个父类别,每个类别都有子类别,如下所示:

  • constructions corollaires 建筑花冠
    • 01 01
    • 02 02
    • 03 03
    • 04 04
  • Between two suns 在两个太阳之间

    • I 一世
    • II II
    • III 三级
    • IV IV

I'm trying to display the parent category on a archive page, and also the children category. 我正在尝试在存档页面上显示父类别,以及子类别。

here is my code for the parent category : 这是我的父类别代码:

<?php 

    $category = get_the_category(); 
    echo $category[0]->name;

?> 

and for the children category : 对于儿童类别:

<?php 

    $category = get_the_category(); 
    echo $category[1]->name;

?> 

what I don't understand, is that for posts who are in inside "Between two suns" category, first code echoes "Between two suns" which is the correct value, but posts who are inside "constructions corollaires", it echoes "I", the sub category instead of "constructions corollaires". 我不明白的是,对于属于“两个太阳之间”类别中的帖子,第一个代码会回显“两个太阳之间”,这是正确的值,但是位于“建筑花冠”中的帖子会回显“我” ”中的子类别,而不是“建筑花冠”。

and it's the same problem with my second code, for posts who are in inside "Between two suns" category, second code echo "I" which is the correct value (1st children category), but posts who are inside "constructions corollaires", it echoes "constructions corollaires", the parent category instead of "01" for example (the children category. 第二个代码也有同样的问题,对于“两个太阳之间”类别中的帖子,第二个代码回显“ I”,这是正确的值(第一个子类别),但是属于“建筑花冠”中的帖子,它回显“ constructions corollaires”,例如,父类别而不是“ 01”(子类别。

I don't understand what I'm doing wrong... 我不明白我在做什么错...

all my categories are sorted in the back end. 我所有的类别都在后端排序。

can anybody help me with this ? 有人可以帮我吗? It looks like an ordering issue but I really don't know how to fix this ! 它看起来像是订购问题,但我真的不知道该如何解决!

For only Top label category 仅针对顶级标签类别

$categories = get_terms( 
'category', 
 array('parent' => 0)
);

And get children category just pass parent category id in parent item 获取子类别只需在父项中传递父类别ID

$categories = get_terms( 
'category', 
array('parent' => $cat->ID)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM