简体   繁体   English

显示自定义分类术语的子级

[英]Show children of custom taxonomy terms

I have the WP Jb manager plugin which I believe is just a custom post type for jobs. 我有WP Jb管理器插件,我相信这只是工作的自定义帖子类型。 I have enabled categories created some categories. 我已经启用类别创建了一些类别。

I need to just show the children of each category instead of the whole list of categories. 我只需要显示每个类别的子代,而不是整个类别列表即可。

I have the following code: 我有以下代码:

<?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>";
}
?>

Which outputs a list of all categories (parent and children) as so: 这样输出所有类别(父级和子级)的列表:

  • Office 办公室
  • Warehouse 仓库
  • Manufacturing 制造业
  • Industrial 产业
  • Construction 施工
  • Building Services Engineers 建筑服务工程师

The parent categories are the bold ones: Office, Industrial and Construction. 父类别为粗体:办公,工业和建筑。 I want to take one of them and display the children of that category only. 我想选择其中一个,仅显示该类别的子级。

For example: get_category('industrial', 'children_of') (I know that's not the correct syntax), so it would result in showing the children only of the industrial category: 例如: get_category('industrial', 'children_of') (我知道这不是正确的语法),因此它将导致仅显示工业类别的子级:

  • Warehouse 仓库
  • Manufacturing 制造业

I can't seem to find a way to do this - Could anyone help? 我似乎找不到解决方法-有人可以帮忙吗?

I managed to do this by using the following code: 我设法通过使用以下代码来做到这一点:

<?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>";
}
?>

You can fetch the parent categories and then construct a list for each subcategory like so: 您可以获取父类别,然后为每个子类别构造一个列表,如下所示:

<?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;

Note that the code wasnt tested. 请注意,该代码未经测试。 Find more info here: https://developer.wordpress.org/reference/functions/get_terms/ 在此处查找更多信息: https : //developer.wordpress.org/reference/functions/get_terms/

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

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