我在建一个<select>
下拉列表显示自定义层次分类法的术语列表。我如何知道每个术语的深度?我想为子术语添加一些缩进(它们嵌套得越深,缩进应该越多)。
但是,我无法从get_terms()
. 如何将深度级别设置为整数?
我在建一个<select>
下拉列表显示自定义层次分类法的术语列表。我如何知道每个术语的深度?我想为子术语添加一些缩进(它们嵌套得越深,缩进应该越多)。
但是,我无法从get_terms()
. 如何将深度级别设置为整数?
您可以使用wp_dropdown_categories():
显示或检索类别的HTML下拉列表。
$args = array(
\'show_count\' => 1,
\'hierarchical\' => 1,
\'taxonomy\' => \'my_taxonomy\',
);
wp_dropdown_categories( $args );
<?php
$args = array(
\'taxonomy\' => \'category\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hierarchical\' => true,
\'hide_empty\' => false,
);
$the_query = new WP_Term_Query($args);
$categories = $the_query->get_terms();
foreach($categories as $cat){
$ancestors = get_ancestors( $cat->term_id, \'category\' );
$cat->ancestors = $ancestors; // array( 0 => 15, 1 => 45 ) - 3rd level term
$cat->depth = count( $ancestors ) ;
}
?>
<select>
<?php
foreach($categories as $cat){
echo \'<option value="\'.$cat->term_id.\'">\'.str_repeat(\' \',$cat->depth).$cat->name.\'</option>\';
}
?>
</select>
我使用以下代码显示存档下拉列表: wp_dropdown_categories( \'taxonomy=week&hierarchical=1&orderby=name\' ); 然而,分类法的格式是第1周、第2周。。。。第10周、第11周我需要按照http://www.php.net/manual/en/function.natsort.php e、 g。第1周第2周<第10周第11周目前正在订购true alpha,例如。第1周第10周第11周第2周不知道最好的方