通过父ID获取分类法的子类别

时间:2019-08-12 10:24:27

标签: wordpress custom-post-type taxonomy custom-taxonomy taxonomy-terms

我有一个带有分类法“ taxoproject”的自定义帖子类型“项目”,并且我有一个带有查询循环的模板页面“ template-project.php”以显示我的帖子。

在此循环中,我需要显示与帖子相关的每个类别。

这是我的代码:

<ul>
   <?php
     $terms = get_the_terms($post->ID, 'taxoproject');
     foreach($terms as $term) { ?>

         <li>
            <?php echo $term->name; ?>
         </li>

     <?php }
   ?>
</ul>

我的代码可以很好地工作,但有一个例外,我希望仅显示此分类法的特定父级的子级(id 76),而我不知道如何进行。

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

<ul>
<?php
  // use taxonomy slug
   $terms = get_terms([
    'taxonomy' => 'taxoproject',
    'hide_empty' => true,
  ]);
 foreach($terms as $term) { ?>

     <li>
        <?php echo $term->post_title;?>

       </li>

      <?php }

?>

答案 1 :(得分:0)

也许此代码对您有用

jetifier-standalone -l verbose -r -i jetified.aar -o ./deJetified.aar

答案 2 :(得分:0)

我找到了解决问题的方法:

<ul>
    <?php
        $taxonomy = 'taxoproject'; // Taxonomy slug.
        $terms = get_the_terms( $post->ID, $taxonomy );

        $children = '';

        foreach ( $terms as $term ) {
            if( $term -> parent == 76 ) { // Parent ID 
                $children = $term->name; ?>

                <li><?php echo $children; ?></li>

            <?php } 
        }
    ?>
</ul>

谢谢大家的帮助!