wp_get_links()
云策文档标注
概述
wp_get_links() 是一个已弃用的 WordPress 函数,用于获取与分类关联的链接,现已由 wp_list_bookmarks() 替代。该函数接受查询字符串参数,并返回链接列表或 null。
关键要点
- wp_get_links() 自 WordPress 2.1.0 起已弃用,建议使用 wp_list_bookmarks() 替代。
- 函数接受一个查询字符串参数 $args,用于指定分类和其他选项。
- 内部处理包括参数解析和默认值设置,最终调用 wp_list_bookmarks() 执行。
- 返回类型为 null 或 string,取决于参数设置。
代码示例
function wp_get_links($args = '') {
_deprecated_function( __FUNCTION__, '2.1.0', 'wp_list_bookmarks()' );
if ( ! str_contains( $args, '=' ) ) {
$cat_id = $args;
$args = add_query_arg( 'category', $cat_id, $args );
}
$defaults = array(
'after' => '',
'before' => '',
'between' => ' ',
'categorize' => 0,
'category' => '',
'echo' => true,
'limit' => -1,
'orderby' => 'name',
'show_description' => true,
'show_images' => true,
'show_rating' => false,
'show_updated' => true,
'title_li' => '',
);
$parsed_args = wp_parse_args( $args, $defaults );
return wp_list_bookmarks($parsed_args);
}注意事项
- 使用此函数会触发 _deprecated_function() 警告,建议在开发中避免使用并迁移到 wp_list_bookmarks()。
- 参数 $args 可以是查询字符串或分类 ID,函数会自动处理格式转换。
- 相关函数包括 wp_list_bookmarks()、_deprecated_function()、wp_parse_args() 和 add_query_arg()。
原文内容
Gets the links associated with category.
Description
See also
Parameters
$argsstringrequired-
a query string
Source
function wp_get_links($args = '') {
_deprecated_function( __FUNCTION__, '2.1.0', 'wp_list_bookmarks()' );
if ( ! str_contains( $args, '=' ) ) {
$cat_id = $args;
$args = add_query_arg( 'category', $cat_id, $args );
}
$defaults = array(
'after' => '<br />',
'before' => '',
'between' => ' ',
'categorize' => 0,
'category' => '',
'echo' => true,
'limit' => -1,
'orderby' => 'name',
'show_description' => true,
'show_images' => true,
'show_rating' => false,
'show_updated' => true,
'title_li' => '',
);
$parsed_args = wp_parse_args( $args, $defaults );
return wp_list_bookmarks($parsed_args);
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Deprecated. Use wp_list_bookmarks() |
| 1.0.1 | Introduced. |