在WordPress后台显示待审文章角标提示

WordPress默认情况下有待审评论时会显示一个角标提示,如果您的主题有前端投稿功能,并且投稿后文章是待审状
2025-05-21 4 分钟阅读 0 次浏览
0 条评论

WordPress默认情况下有待审评论时会显示一个角标提示,如果您的主题有前端投稿功能,并且投稿后文章是待审状态,也想有一个角标提示,及时提醒管理员审核通过这些文章,可以参考本文的方法。

在WordPress后台显示待审文章角标提示

将下面代码添加到当前主题函数模板functions.php中:

为全部文章类型显示待审角标提示

复制代码

function zm_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', 'zm_menu_badges_for_all_post_types' );

一般直接用上面的代码即可,如何有特殊的要求可以参考下面的代码:

仅为一个特定的自定义文章类型显示待审角标提示

复制代码

// 仅针对自定义文章类型 'book'添加菜单计数器function zm_menu_badges_for_book() {	global $menu;	$pending_count = wp_count_posts( 'book' )->pending;	if ( $pending_count > 0 ) {		foreach ( $menu as $key => $item ) {			if ( $item[2] == 'edit.php?post_type=book' ) {				$menu[$key][0] .= sprintf( '<span class="menu-counter count-%1$d" title="%1$d 篇待审"><span class="count">%1$d</span></span>', $pending_count ); 				break;			}		}	}}add_action('admin_menu', 'zm_menu_badges_for_book');

为所有自定义文章类型显示待审角标提示

复制代码

function zm_menu_badges_for_all_post_types() {    global $menu;     // 获取所有已注册的文章类型    $post_types = get_post_types( array( 'public' => true ), 'objects' );     foreach ( $post_types as $post_type ) {        // 获取待审文章数量        $pending_count = wp_count_posts( $post_type->name )->pending;         // 如果有待审文章,则添加计数器        if ( $pending_count > 0 ) {            foreach ( $menu as $key => $item ) {                // 找到对应的编辑页面菜单项                if ( $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                    );                    break;                }            }        }    }}add_action( 'admin_menu', 'zm_menu_badges_for_all_post_types' );

本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!

分享文章

暂无评论

欢迎发表您的观点和想法

发表评论

您的邮箱地址不会被公开。必填项已用 * 标注