我有WP管理插件,我相信这只是一个定制的职位类型的工作。我启用了类别,创建了一些类别。
我只需要显示每个类别的子类,而不是整个类别的列表。
我有以下代码:
<?php
$terms = get_terms( 'job_listing_category', 'orderby=count&hide_empty=0' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>它输出所有类别(父类和子类)的列表如下:
父类别是大胆的:办公、工业和建筑。我想拿其中之一,只展示这一类的孩子。
例如:get_category('industrial', 'children_of') (我知道这不是正确的语法),因此它将只显示工业类别的子类:
我似乎想不出办法来-有人能帮忙吗?
发布于 2018-06-16 19:58:53
通过使用以下代码,我成功地做到了这一点:
<?php
$terms = get_terms( 'job_listing_category', 'parent=59' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>发布于 2018-06-16 16:54:45
您可以获取父类别,然后为每个子类别构造一个列表,如下所示:
<?php
$taxonomies = get_terms(array(
'taxonomy' => 'job_listing_category',
'hide_empty' => false,
'parent' => 0,
));
if (!empty($taxonomies)):
foreach ($taxonomies as $parent) {
$output = '<ul>';
$children = get_terms(array(
'taxonomy' => 'job_listing_category',
'parent' => $parent->term_id,
'hide_empty' => false,
));
foreach ($children as $child) {
$output .= '<li>' . esc_html($child->name) . '</li>';
}
$output = '</ul>';
}
echo $output;
endif;注意,代码没有经过测试。在这里可以找到更多信息:terms/
https://stackoverflow.com/questions/50888658
复制相似问题