get_most_active_blogs()
云策文档标注
概述
get_most_active_blogs() 是一个已弃用的 WordPress 函数,用于检索最活跃站点的列表。该函数在 3.0.0 版本中被标记为弃用,主要依赖于其他弃用函数如 get_blog_list() 来实现功能。
关键要点
- 函数已弃用:自 WordPress 3.0.0 起,使用 _deprecated_function() 标记为弃用,开发者应避免在新代码中使用。
- 功能:根据文章数量(postcount)排序,返回最活跃站点的数组,可选是否直接显示列表。
- 参数:$num(整数,默认 10)控制返回站点数量;$display(布尔值,默认 true)控制是否输出显示。
- 返回值:返回一个包含最活跃站点信息的数组,使用 array_slice() 截取指定数量。
代码示例
function get_most_active_blogs( $num = 10, $display = true ) {
_deprecated_function( __FUNCTION__, '3.0.0' );
// 内部逻辑:获取博客列表、按文章数排序、处理显示和返回
}注意事项
- 依赖弃用函数:内部调用 get_blog_list(),该函数也已弃用,可能导致兼容性问题。
- 替代方案:建议使用现代 WordPress 多站点 API 或自定义查询来替代此功能。
- 显示输出:当 $display 为 true 时,会直接输出 HTML 内容,可能不符合当前开发最佳实践。
原文内容
Deprecated functionality to retrieve a list of the most active sites.
Parameters
$numintoptional-
Number of activate blogs to retrieve.
Default:
10 $displaybooloptional-
Whether or not to display the most active blogs list.
Default:
true
Source
function get_most_active_blogs( $num = 10, $display = true ) {
_deprecated_function( __FUNCTION__, '3.0.0' );
$blogs = get_blog_list( 0, 'all', false ); // $blog_id -> $details
if ( is_array( $blogs ) ) {
reset( $blogs );
$most_active = array();
$blog_list = array();
foreach ( (array) $blogs as $key => $details ) {
$most_active[ $details['blog_id'] ] = $details['postcount'];
$blog_list[ $details['blog_id'] ] = $details; // array_slice() removes keys!
}
arsort( $most_active );
reset( $most_active );
$t = array();
foreach ( (array) $most_active as $key => $details ) {
$t[ $key ] = $blog_list[ $key ];
}
unset( $most_active );
$most_active = $t;
}
if ( $display ) {
if ( is_array( $most_active ) ) {
reset( $most_active );
foreach ( (array) $most_active as $key => $details ) {
$url = esc_url('http://' . $details['domain'] . $details['path']);
echo '<li>' . $details['postcount'] . " <a href='$url'>$url</a></li>";
}
}
}
return array_slice( $most_active, 0, $num );
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Deprecated. |
| MU (3.0.0) | Introduced. |