您可以尝试以下代码:
$parent_cat = get_query_var(\'cat\');
wp_list_categories("child_of=$parent_cat&show_count=1");
您将在输出中获得顶级类别的列表
如果需要选择类别的示例,可以执行以下操作:
$args = array(
\'taxonomy\' => \'tags\',
\'parent\' => 0, // get top level categories
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hierarchical\' => 1,
\'pad_counts\' => 0
);
$categories = get_categories( $args );
echo \'<select name="category_id">\';
foreach ( $categories as $category ){
echo \'<option value="\'.$category->ID.\'">\'.$category->name.\'</option>\';
}
echo \'</select>\';
提交表单后,您需要处理POST请求:
wp_set_post_categories( $your_record_id, array( $_POST[\'category_id\'] ), true);