dashboard_glance_items
云策文档标注
概述
dashboard_glance_items 是一个 WordPress 过滤器,用于修改“概览”仪表板小部件中显示的额外元素数组。该小部件在 3.8.0 版本前名为“Right Now”。
关键要点
- 过滤器名称:dashboard_glance_items
- 参数:$items(字符串数组),表示额外“概览”小部件项目的数组
- 用途:允许开发者添加自定义内容(如自定义文章类型计数)到仪表板“概览”小部件中
- 输出格式:每个元素在输出时被包裹在列表项标签中
- 相关函数:wp_dashboard_right_now(),用于显示站点基本统计信息
- 版本历史:自 WordPress 3.8.0 引入
代码示例
function wpdocs_add_custom_post_counts() {
$post_types = array( 'wpdocs-project' );
foreach ( $post_types as $cpt ) {
$cpt_info = get_post_type_object( $cpt );
$num_posts = wp_count_posts( $cpt );
$num = number_format_i18n( $num_posts->publish );
$text = _n( $cpt_info->labels->singular_name, $cpt_info->labels->name, intval( $num_posts->publish ) );
echo '<li class="' . $cpt_info->name . '-count">' . $num . ' ' . $text . '</li>';
}
}
add_action( 'dashboard_glance_items', 'wpdocs_add_custom_post_counts' );注意事项
- 注意:dashboard_glance_items 是一个过滤器(filter),不是动作(action),但示例代码中使用了 add_action,这可能是一个错误或特定用法,需根据实际上下文确认。
原文内容
Filters the array of extra elements to list in the ‘At a Glance’ dashboard widget.
Description
Prior to 3.8.0, the widget was named ‘Right Now’. Each element is wrapped in list-item tags on output.
Parameters
$itemsstring[]-
Array of extra ‘At a Glance’ widget items.
Source
$elements = apply_filters( 'dashboard_glance_items', array() );
Changelog
| Version | Description |
|---|---|
| 3.8.0 | Introduced. |
Skip to note 2 content
Gerald
Add Custom Post Type to ‘At a Glance’ dashboard widget:
function wpdocs_add_custom_post_counts() { $post_types = array( 'wpdocs-project' ); foreach ( $post_types as $cpt ) { $cpt_info = get_post_type_object( $cpt ); $num_posts = wp_count_posts( $cpt ); $num = number_format_i18n( $num_posts->publish ); $text = _n( $cpt_info->labels->singular_name, $cpt_info->labels->name, intval( $num_posts->publish ) ); echo '<li class="page-count '. esc_attr( $cpt_info->name ) . '-count"><a href="edit.php?post_type=' . esc_attr( $cpt ) . '">' . $num . ' ' . $text . '</a></li>'; } } add_action( 'dashboard_glance_items', 'wpdocs_add_custom_post_counts' );The Custom Post Type is shown with the same style as default Posts, Pages and Comments use, including number of posts and link to CPT list.
dashboard_glance_itemsas action – but it is a filter and not an action!