【问题标题】:Get only first level child categories of current category仅获取当前类别的一级子类别
【发布时间】:2015-03-08 20:43:40
【问题描述】:

我为 CTP 创建了分类档案模板。 在此页面中,在列出当前分类的所有帖子之前 - 我想列出当前分类的所有子类别。

我设法显示的是该分类的子类别的完整列表,但我需要过滤掉第二级子类别。

例如:
-分类学
--child-taxonomy1
--child-taxonomy2
----child-taxonomy2的孩子

我只想显示“child-taxonomy1”和“child-taxonomy2”

到目前为止,这是我的代码:http://www.codeshare.io/MQ4YT

【问题讨论】:

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


    【解决方案1】:

    使用此代码

    <ul> 
    <?php
    global $post;
    // grab terms of current post, replace taxonomy name
    $terms = get_the_terms($post->ID, 'name_of_custom_taxonomy');
    // define arguments of following listing function
    $args = array (
        'child_of' => $terms[0], // current post's (first) category 
        'title_li' => '', // disable display of outer list item 
        'taxonomy' => 'name_of_custom_taxonomy' // replace as well
    );
    // list child categories
    wp_list_categories($args);
    ?>
    </ul>
    

    【讨论】:

    • 你看我的代码了吗?我正在尝试进入分类档案页面,而不是在帖子中。
    【解决方案2】:
    $taxonomy_name = 'product-category';
                $queried_object = get_queried_object();
                $term_id = $queried_object->term_id;
    
                $termchildren = get_terms( $taxonomy_name, array( 'parent' => $term_id, 'hide_empty' => false ) );
    
                echo '<ul>';
                foreach ( $termchildren as $child ) {
                    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $child->name . '</a></li>';
                }
                echo '</ul>';
    

    像魅力一样工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多