仅显示父母的顶级孩子(Wordpress)

时间:2013-10-14 04:04:49

标签: php wordpress

嘿伙计们,我是wordpress的新手,想知道如何只显示父母的顶级孩子。我的意思是,让我们说 电影是最重要的类别,其中的孩子是迪士尼,皮克斯等,而迪士尼的孩子是动作而皮克斯也是动作 在电影类别中,我只想展示迪士尼,皮克斯不是动作 所以我们这样说:

Movies - Main
-Disney (Displayed in Movies Category)
--Action NOT DISPLAYED
-Pixar (Displayed in Movies Category)
--Action NOT DISPLAYED

但是wordpress会自动显示它。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

为此,您将首先使用get_categories获取所有顶级类别,然后获取将每个顶级类别作为父级的所有类别:

$args = array(
        'orderby' => 'name',
        'parent' => 0
    );
$top_categories = get_categories( $args );
foreach ($top_categories as $top_category) {
    //here you display info about the top category or anything you want to do with it
    echo '<a href="' . get_category_link( $top_category->term_id ) . '">' . $top_category->name . '</a><br/>';
    $args = array(
            'orderby' => 'name',
            'parent' => $top_category->term_id
        );
    $child_categories = get_categories( $args );
    foreach ($child_categories as $child_category) {
    //here you write the code for child categories
        echo '<a href="' . get_category_link( $child_category->term_id ) . '">' . $child_category->name . '</a><br/>';
    }
}