只列出帖子所在的子类别,属于WordPress中的特定父类别

时间:2017-01-25 作者:pandu

我正在建立一个报价网站,我有两个主要类别,如Topic &;Authors
每个引用都有作者名称和主题名称,这些是主题的子类别(&;作者
AND my question is
我只想显示作者类别,它是作者类别的子类别请帮助我。。。

Topic
-爱情
-生活
-朋友等
Authors
-Author1
-Author2
-Author3

2 个回复
SO网友:BlueSuiter

它将为您提供如上所示的相同结构:

wp_list_categories(array(\'hide_empty\'=>0, \'hierarchical\' => true, \'include\' => array(\'12\',\'12\')));

SO网友:pandu

    <?php
        // names / IDs of parents
        $parents = array(
            \'par2\' => 76
        );
        // get post categories
        $categories = get_the_terms( $post->ID, \'category\' );
        // output top level cats and their children
        foreach( $parents as $parent_name => $parent_id ):
        // output parent name and link
        //echo \'<a href="\' . get_term_link( $parent_id, \'category\' ) . \'">\' . $parent_name . \'</a>: \';
        // initialize array to hold child links
        $links = array();
        foreach( $categories as $category ):
        if( $parent_id == $category->parent ):
        // put link in array
        $links[] = \'<li><a href="\' . get_term_link( $category ) . \'">\' . $category->name . \'</a></li>\';
        endif;
        endforeach;
        // join and output links with separator
        echo join($links );
        endforeach;            
    ?>
这就是我想要的答案谢谢你的回复