WordPress后台待审文章显示气泡提示的方法

追格_咖小啡/ 08月21日/ WordPress/ 浏览 739

之前小编分享过一篇《WordPress 后台菜单添加 badge-红点提示(气泡通知)》的文章,今天我简单说说待审文章(为全部文章类型显示待审角标提示)的气泡提示如何操作。


流程还是一如既往的简单,将下方代码添加到当前WordPress主题函数文件:functions.php中即可。


function zhuige_menu_badges_for_all_post_types() {
    global $menu;
 
    // 所有已注册的文章类型
    $post_types = get_post_types( array( 'public' => true ), 'objects' );
 
    foreach ( $post_types as $post_type ) {
        // 排除附件attachment类型
        if ( $post_type->name === 'attachment' ) {
            continue;
        }
 
        // 获取待审文章的数量
        $pending_count = wp_count_posts( $post_type->name )->pending;
 
        // 如果有待审文章,添加气泡计数器
        if ( $pending_count > 0 ) {
            foreach ( $menu as $key => $item ) {
                // 找到对应的编辑页面菜单项
                if ( $post_type->name === 'post' && $item[2] === 'edit.php' ) {
                    // 添加带有title标签的计数器,使用 "Posts" 作为标题
                    $menu[$key][0] .= sprintf(
                        '<span class="menu-counter count-%1$d" title="%1$d 篇待审文章"><span class="count">%1$d</span></span>',
                        $pending_count
                    );
                } elseif ( $item[2] == 'edit.php?post_type=' . $post_type->name ) {
                    // 添加带有title标签的计数器,使用文章类型的名称作为标题
                    $menu[$key][0] .= sprintf(
                        '<span class="menu-counter count-%1$d" title="%1$d 篇待审 %2$s"><span class="count">%1$d</span></span>',
                        $pending_count,
                        $post_type->labels->name
                    );
                }
            }
        }
    }
}
add_action( 'admin_menu', 'zhuige_menu_badges_for_all_post_types' );

发表评论

暂无评论,抢个沙发...

客服 工单