不知道大家是否留意到,WordPress 后台仪表盘的“概率”小工具中仅显示有多少篇文章、多少个页面和多少条评论,而我们的自定义文章类型却不显示,对于有点强迫症的博主站长们而言,这个是无法忍受的,所以今天 boke112 就教大家如何在这个“概览”小工具中显示自定义文章类型。
WordPress 后台仪表盘“概览”显示自定义文章类型的做法很简单,只需要将以下代码添加到主题的 functions.php 文件最后一个 ?> 的前面即可。
- /**
- * 后台仪表盘【概览】显示自定义文章类型
- * https://boke112.com/4022.html
- */
- add_filter( ‘dashboard_glance_items’, ‘custom_glance_items’, 10, 1 );
- function custom_glance_items( $items = array() ) {
- foreach( array( ‘bulletin’, ‘video’ ) as $zdy_type ) {
- $num_posts = wp_count_posts( $zdy_type );
- $post_type_object = get_post_type_object( $zdy_type );
- if ( $num_posts && $num_posts->publish ) {
- $text = _n( ‘%s 个’ . $post_type_object->labels->singular_name, ‘%s 个’ . $post_type_object->labels->name, $published);
- $text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
- if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
- printf( ‘<li class=“%1$s-count”><a href=“edit.php?post_type=%1$s”>%2$s</a></li>’, $zdy_type, $text );
- } else {
- printf( ‘<li class=“%1$s-count”><span>%2$s</span></li>’, $zdy_type, $text );
- }
- }
- }
- return $items;
- }
其中 array( ‘bulletin’, ‘video’ ) 里面的 bulletin 和 video 就是自定义文章类型,请自行修改。如果有更多类型的话,就继续添加即可。这些自定义文章类型的默认单位都是“个”,如果想显示其他单位,比如“条”或其他的,可以将以上代码的第 11 行修改为:
- if ( ‘bulletin’ == $zdy_type ) {
- $text = _n( ‘%s 条’ . $post_type_object->labels->singular_name, ‘%s 条’ . $post_type_object->labels->name, $published);
- } else {
- $text = _n( ‘%s 个’ . $post_type_object->labels->singular_name, ‘%s 个’ . $post_type_object->labels->name, $published);
- }
多个自定义文章类型和多个不同的单位,就根据以上格式继续添加判断条件即可。同理,如果自定义文章类型的图标不规范,导致前面的图标都是一样的话,也可以用这个方法添加不同的图标。