Php 仅显示父级的顶级子级(Wordpress)
嘿,伙计们,我是wordpress的新手,我想知道如何只显示父级的顶级子级。我的意思是说 电影是顶级类别,它的孩子是迪斯尼、皮克斯等,迪斯尼的孩子是动作,皮克斯也是动作 在电影类别中,我只想展示迪斯尼,皮克斯不想展示动作 让我们这样说:Php 仅显示父级的顶级子级(Wordpress),php,wordpress,Php,Wordpress,嘿,伙计们,我是wordpress的新手,我想知道如何只显示父级的顶级子级。我的意思是说 电影是顶级类别,它的孩子是迪斯尼、皮克斯等,迪斯尼的孩子是动作,皮克斯也是动作 在电影类别中,我只想展示迪斯尼,皮克斯不想展示动作 让我们这样说: Movies - Main -Disney (Displayed in Movies Category) --Action NOT DISPLAYED -Pixar (Displayed in Movies Category) --Action NOT DISP
Movies - Main
-Disney (Displayed in Movies Category)
--Action NOT DISPLAYED
-Pixar (Displayed in Movies Category)
--Action NOT DISPLAYED
但是wordpress会自动显示它。如何解决此问题?为此,您必须首先使用获取所有顶级类别,然后获取所有以每个顶级类别为父类别的类别:
$args = array(
'orderby' => 'name',
'parent' => 0
);
$top_categories = get_categories( $args );
foreach ($top_categories as $top_category) {
//here you display info about the top category or anything you want to do with it
echo '<a href="' . get_category_link( $top_category->term_id ) . '">' . $top_category->name . '</a><br/>';
$args = array(
'orderby' => 'name',
'parent' => $top_category->term_id
);
$child_categories = get_categories( $args );
foreach ($child_categories as $child_category) {
//here you write the code for child categories
echo '<a href="' . get_category_link( $child_category->term_id ) . '">' . $child_category->name . '</a><br/>';
}
}
$args=array(
'orderby'=>'name',
“父项”=>0
);
$top\u categories=get\u categories($args);
foreach($top\u类别作为$top\u类别){
//在这里,您可以显示有关顶级类别或任何您想使用它的信息
回声“
”;
$args=数组(
'orderby'=>'name',
“父项”=>$top\u类别->术语\u id
);
$child\u categories=get\u categories($args);
foreach($child\u categories作为$child\u categories){
//在这里,您编写了子类别的代码
回声“
”;
}
}
这可能会有所帮助