是的,您可以使用get_categories() 使用\'child_of\'
属性例如,ID为17的类别的所有子类别:
$args = array(\'child_of\' => 17);
$categories = get_categories( $args );
foreach($categories as $category) {
echo \'<p>Category: <a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a> </p> \';
echo \'<p> Description:\'. $category->description . \'</p>\';
echo \'<p> Post Count: \'. $category->count . \'</p>\';
}
这将获得作为子代的所有类别(即子代和孙代)。
如果要仅显示直接子类的类别(即仅限子类),可以使用\'parent\'
属性
$args = array(\'parent\' => 17);
$categories = get_categories( $args );
foreach($categories as $category) {
echo \'<p>Category: <a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a> </p> \';
echo \'<p> Description:\'. $category->description . \'</p>\';
echo \'<p> Post Count: \'. $category->count . \'</p>\';
}