get_links()
云策文档标注
概述
get_links() 是一个已弃用的 WordPress 函数,用于根据分类 ID 获取关联的链接。它已被 get_bookmarks() 替代,主要用于向后兼容。
关键要点
- 函数已弃用:自 WordPress 2.1.0 起,建议使用 get_bookmarks() 替代。
- 功能:通过分类 ID 获取链接,支持多种输出控制参数,如排序、显示图像、描述等。
- 参数:包括 $category(分类 ID,默认 -1 表示所有)、$before、$after、$between、$show_images、$orderby、$show_description、$show_rating、$limit、$show_updated 和 $display。
- 返回值:根据 $display 参数决定是直接输出 HTML 还是返回字符串。
- 内部实现:函数内部调用 get_bookmarks() 并处理参数映射,如 $orderby 支持反转排序(以 '_' 开头)。
代码示例
function get_links($category = -1, $before = '', $after = '', $between = ' ', $show_images = true, $orderby = 'name',
$show_description = true, $show_rating = false, $limit = -1, $show_updated = 1, $display = true) {
_deprecated_function( __FUNCTION__, '2.1.0', 'get_bookmarks()' );
$order = 'ASC';
if ( str_starts_with($orderby, '_') ) {
$order = 'DESC';
$orderby = substr($orderby, 1);
}
if ( $category == -1 ) // get_bookmarks() uses '' to signify all categories.
$category = '';
$results = get_bookmarks(array('category' => $category, 'orderby' => $orderby, 'order' => $order, 'show_updated' => $show_updated, 'limit' => $limit));
if ( !$results )
return;
$output = '';
foreach ( (array) $results as $row ) {
// 处理链接输出逻辑
$output .= $before;
// ... 更多代码
$output .= "$aftern";
}
if ( !$display )
return $output;
echo $output;
}注意事项
- 由于已弃用,新开发中应避免使用此函数,改用 get_bookmarks()。
- 参数 $orderby 支持以 '_' 开头表示降序排序,例如 '_name' 会按名称降序排列。
- 函数内部依赖多个辅助函数,如 esc_url()、esc_attr() 和 sanitize_bookmark_field(),确保输出安全。
原文内容
Gets the links associated with category by ID.
Description
See also
Parameters
$categoryintoptional-
The category to use. If no category supplied uses all.
Default 0.Default:
-1 $beforestringoptional-
The HTML to output before the link. Default empty.
$afterstringoptional-
The HTML to output after the link. Default
<br />. $betweenstringoptional-
The HTML to output between the link/image and its description.
Not used if no image or $show_images is true. Default ‘ ‘. $show_imagesbooloptional-
Whether to show images (if defined).
Default:
true $orderbystringoptional-
The order to output the links. E.g.
'id','name','url','description','rating', or'owner'. Default'name'.
If you start the name with an underscore, the order will be reversed.
Specifying'rand'as the order will return links in a random order. $show_descriptionbooloptional-
Whether to show the description if show_images=false/not defined.
Default:
true $show_ratingbooloptional-
Show rating stars/chars.
Default:
false $limitintoptional-
Limit to X entries. If not specified, all entries are shown.
Default:
-1 $show_updatedintoptional-
Whether to show last updated timestamp.
Default:
1 $displaybooloptional-
Whether to display the results, or return them instead.
Default:
true
Source
function get_links($category = -1, $before = '', $after = '<br />', $between = ' ', $show_images = true, $orderby = 'name',
$show_description = true, $show_rating = false, $limit = -1, $show_updated = 1, $display = true) {
_deprecated_function( __FUNCTION__, '2.1.0', 'get_bookmarks()' );
$order = 'ASC';
if ( str_starts_with($orderby, '_') ) {
$order = 'DESC';
$orderby = substr($orderby, 1);
}
if ( $category == -1 ) // get_bookmarks() uses '' to signify all categories.
$category = '';
$results = get_bookmarks(array('category' => $category, 'orderby' => $orderby, 'order' => $order, 'show_updated' => $show_updated, 'limit' => $limit));
if ( !$results )
return;
$output = '';
foreach ( (array) $results as $row ) {
if ( !isset($row->recently_updated) )
$row->recently_updated = false;
$output .= $before;
if ( $show_updated && $row->recently_updated )
$output .= get_option('links_recently_updated_prepend');
$the_link = '#';
if ( !empty($row->link_url) )
$the_link = esc_url($row->link_url);
$rel = $row->link_rel;
if ( '' != $rel )
$rel = ' rel="' . $rel . '"';
$desc = esc_attr(sanitize_bookmark_field('link_description', $row->link_description, $row->link_id, 'display'));
$name = esc_attr(sanitize_bookmark_field('link_name', $row->link_name, $row->link_id, 'display'));
$title = $desc;
if ( $show_updated )
if ( !str_starts_with($row->link_updated_f, '00') )
$title .= ' ('.__('Last updated') . ' ' . gmdate(get_option('links_updated_date_format'), $row->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)) . ')';
if ( '' != $title )
$title = ' title="' . $title . '"';
$alt = ' alt="' . $name . '"';
$target = $row->link_target;
if ( '' != $target )
$target = ' target="' . $target . '"';
$output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
if ( '' != $row->link_image && $show_images ) {
if ( str_contains( $row->link_image, 'http' ) )
$output .= '<img src="' . $row->link_image . '"' . $alt . $title . ' />';
else // If it's a relative path.
$output .= '<img src="' . get_option('siteurl') . $row->link_image . '"' . $alt . $title . ' />';
} else {
$output .= $name;
}
$output .= '</a>';
if ( $show_updated && $row->recently_updated )
$output .= get_option('links_recently_updated_append');
if ( $show_description && '' != $desc )
$output .= $between . $desc;
if ($show_rating) {
$output .= $between . get_linkrating($row);
}
$output .= "$aftern";
} // End while.
if ( !$display )
return $output;
echo $output;
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Deprecated. Use get_bookmarks() |
| 0.71 | Introduced. |