腾讯云服务器优惠活动华纳云香港服务器

WordPress后台仪表盘“概览”显示自定义文章类型

WordPress主题推荐

不知道大家是否留意到,WordPress 后台仪表盘的“概率”小工具中仅显示有多少篇文章、多少个页面和多少条评论,而我们的自定义文章类型却不显示,对于有点强迫症的博主站长们而言,这个是无法忍受的,所以今天 boke112 就教大家如何在这个“概览”小工具中显示自定义文章类型。

WordPress后台仪表盘“概览”显示自定义文章类型-第1张-boke112百科(boke112.com)

WordPress 后台仪表盘“概览”显示自定义文章类型的做法很简单,只需要将以下代码添加到主题的 functions.php 文件最后一个 ?> 的前面即可。

  1. /**
  2.  * 后台仪表盘【概览】显示自定义文章类型
  3.  * https://boke112.com/4022.html
  4.  */
  5. add_filter( ‘dashboard_glance_items’, ‘custom_glance_items’, 10, 1 );
  6. function custom_glance_items( $items = array() ) {
  7.     foreacharray( ‘bulletin’, ‘video’ ) as $zdy_type ) {
  8.         $num_posts = wp_count_posts( $zdy_type );
  9.         $post_type_object = get_post_type_object( $zdy_type );
  10.         if ( $num_posts && $num_posts->publish ) {
  11.             $text = _n( ‘%s 个’ . $post_type_object->labels->singular_name, ‘%s 个’ . $post_type_object->labels->name, $published);
  12.             $text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
  13.             if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
  14.                 printf( ‘<li class=“%1$s-count”><a href=“edit.php?post_type=%1$s”>%2$s</a></li>’, $zdy_type$text );
  15.             } else {
  16.                 printf( ‘<li class=“%1$s-count”><span>%2$s</span></li>’, $zdy_type$text );
  17.             }
  18.         }
  19.     }
  20.     return $items;
  21. }

其中 array( ‘bulletin’, ‘video’ ) 里面的 bulletin 和 video 就是自定义文章类型,请自行修改。如果有更多类型的话,就继续添加即可。这些自定义文章类型的默认单位都是“个”,如果想显示其他单位,比如“条”或其他的,可以将以上代码的第 11 行修改为:

  1. if ( ‘bulletin’ == $zdy_type ) {
  2.     $text = _n( ‘%s 条’ . $post_type_object->labels->singular_name, ‘%s 条’ . $post_type_object->labels->name, $published);
  3. else {
  4.     $text = _n( ‘%s 个’ . $post_type_object->labels->singular_name, ‘%s 个’ . $post_type_object->labels->name, $published);
  5. }

多个自定义文章类型和多个不同的单位,就根据以上格式继续添加判断条件即可。同理,如果自定义文章类型的图标不规范,导致前面的图标都是一样的话,也可以用这个方法添加不同的图标。

拓展阅读

本文地址:https://boke112.com/post/4022.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
香港云主机55元/年
wu