我有一些主题内置的自定义分类法,在分类法下拉列表中,分类法没有显示在层次结构中。它只显示所有分类法的下拉列表,但不是按层次顺序排列。
这就是我所拥有的:
register_taxonomy(
'recipesets',
'recipe',
array(
'public'=>true,
'hierarchical' => true,
'labels'=> $labels,
'query_var' => 'recipesets',
'show_ui' => true,
'rewrite' => array( 'slug' => 'recipesets', 'with_front' => false ),
)
);
}
并致电:
<label for="edit-title" class="control-label"><i class="fa fa-folder-o"></i><?php _e('Category:', 'agrg') ?></label>
<select name="cat" id="cat" class="postform">
<?php
$terms = get_terms("recipesets", "orderby=count&hide_empty=0");
if ( !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
echo "<option value='" . $term->name . "'>" . $term->name . "</option>";
}
}
?>
</select>
怎么办?
答案 0 :(得分:1)
我就是这样做的:
<?php
/** The taxonomy we want to parse */
$taxonomy = "category";
/** Get all taxonomy terms */
$terms = get_terms($taxonomy, array(
"orderby" => "count",
"hide_empty" => false
)
);
/** Get terms that have children */
$hierarchy = _get_term_hierarchy($taxonomy);
?>
<select name="terms" id="terms">
<?php
/** Loop through every term */
foreach($terms as $term) {
/** Skip term if it has children */
if($term->parent) {
continue;
}
echo '<option value="' . $term->name . '">' . $term->name . '</option>';
/** If the term has children... */
if($hierarchy[$term->term_id]) {
/** ...display them */
foreach($hierarchy[$term->term_id] as $child) {
/** Get the term object by its ID */
$child = get_term($child, "category");
echo '<option value="' . $term->name . '"> - ' . $child->name . '</option>';
}
}
}
?>
</select>
答案 1 :(得分:0)
为什么不使用Wordpress函数wp_dropdown_categories()来为您完成所有工作
$tax_args = array(
'taxonomy' => 'destination',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
);
wp_dropdown_categories($tax_args);