wp_dropdown_cats()
云策文档标注
概述
wp_dropdown_cats() 是一个已弃用的 WordPress 函数,用于生成分类下拉控件。自 WordPress 3.0.0 起,建议使用 wp_dropdown_categories() 替代。
关键要点
- 函数已弃用:自 WordPress 3.0.0 起标记为弃用,应改用 wp_dropdown_categories()。
- 功能:生成分类下拉控件,支持递归显示分类层级。
- 参数:包括 $current_cat(当前分类 ID)、$current_parent(当前父分类 ID)、$category_parent(父分类 ID)、$level(层级深度)、$categories(分类数组)。
- 返回值:成功时返回 void,无分类时返回 false。
- 内部实现:使用 get_categories() 获取分类,递归调用自身以处理子分类。
代码示例
function wp_dropdown_cats( $current_cat = 0, $current_parent = 0, $category_parent = 0, $level = 0, $categories = 0 ) {
_deprecated_function( __FUNCTION__, '3.0.0', 'wp_dropdown_categories()' );
if (!$categories )
$categories = get_categories( array('hide_empty' => 0) );
if ( $categories ) {
foreach ( $categories as $category ) {
if ( $current_cat != $category->term_id && $category_parent == $category->parent) {
$pad = str_repeat( '– ', $level );
$category->name = esc_html( $category->name );
echo "nt<option value='$category->term_id'";
if ( $current_parent == $category->term_id )
echo " selected='selected'";
echo ">$pad$category->name</option>";
wp_dropdown_cats( $current_cat, $current_parent, $category->term_id, $level +1, $categories );
}
}
} else {
return false;
}
}注意事项
- 弃用警告:使用此函数会触发 _deprecated_function() 警告,建议更新代码以避免未来兼容性问题。
- 替代函数:优先使用 wp_dropdown_categories(),它提供更现代和灵活的选项。
- 参数默认值:所有参数都有默认值,但 $categories 为 0 时会自动调用 get_categories() 获取。
原文内容
Legacy function used for generating a categories drop-down control.
Description
See also
Parameters
$current_catintoptional-
ID of the current category. Default 0.
$current_parentintoptional-
Current parent category ID. Default 0.
$category_parentintoptional-
Parent ID to retrieve categories for. Default 0.
$levelintoptional-
Number of levels deep to display. Default 0.
$categoriesarrayoptional-
Categories to include in the control. Default 0.
Source
function wp_dropdown_cats( $current_cat = 0, $current_parent = 0, $category_parent = 0, $level = 0, $categories = 0 ) {
_deprecated_function( __FUNCTION__, '3.0.0', 'wp_dropdown_categories()' );
if (!$categories )
$categories = get_categories( array('hide_empty' => 0) );
if ( $categories ) {
foreach ( $categories as $category ) {
if ( $current_cat != $category->term_id && $category_parent == $category->parent) {
$pad = str_repeat( '– ', $level );
$category->name = esc_html( $category->name );
echo "nt<option value='$category->term_id'";
if ( $current_parent == $category->term_id )
echo " selected='selected'";
echo ">$pad$category->name</option>";
wp_dropdown_cats( $current_cat, $current_parent, $category->term_id, $level +1, $categories );
}
}
} else {
return false;
}
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Deprecated. Use wp_dropdown_categories() |
| 1.2.0 | Introduced. |