题
我一直在努力获得待处理的数量,以出现在管理侧栏上,以供待处理的帖子,例如出现用于待处理评论的小气泡:
无关: :我是唯一认为这应该是核心行为的人吗?我应该在哪里建议此功能?
无论如何,我发现 这个插件, ,但我注意到它并不总是有效。有时,通知器出现在页面或其他项目上。
它用来添加待处理计数的代码是这样的:
$menu[5][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
因此,显然问题是那里的硬编码5,但是我该如何更新它,以便它总是指向帖子?
如果我们知道答案,我将很高兴将此更改提交给插件。
谢谢!
解决方案
@ign
用以下内容替换您发布的代码行。
foreach( $menu as $menu_key => $menu_data ) :
if( 'edit.php' != $menu_data[2] )
continue;
$menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
endforeach;
..那 应该 避免需要知道特定键的需要..(让我知道是否有问题)。
希望会有所帮助.. :)
其他提示
作为对T31OS答案的后续,这是所需的完整代码(将提到的插件内容与T31OS'FIX的内容结合在一起),并修改以处理自定义邮政类型:
add_filter( 'add_menu_classes', 'show_pending_number');
function show_pending_number( $menu ) {
$type = "animals";
$status = "pending";
$num_posts = wp_count_posts( $type, 'readable' );
$pending_count = 0;
if ( !empty($num_posts->$status) )
$pending_count = $num_posts->$status;
// build string to match in $menu array
if ($type == 'post') {
$menu_str = 'edit.php';
} else {
$menu_str = 'edit.php?post_type=' . $type;
}
// loop through $menu items, find match, add indicator
foreach( $menu as $menu_key => $menu_data ) {
if( $menu_str != $menu_data[2] )
continue;
$menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
}
return $menu;
}
将其放入functions.php中,无需插件。
我对Somatic的帖子进行了轻微的更改,该帖子允许多种帖子类型:
// Add pending numbers to post types on admin menu
function show_pending_number($menu) {
$types = array("post", "page", "custom-post-type");
$status = "pending";
foreach($types as $type) {
$num_posts = wp_count_posts($type, 'readable');
$pending_count = 0;
if (!empty($num_posts->$status)) $pending_count = $num_posts->$status;
if ($type == 'post') {
$menu_str = 'edit.php';
} else {
$menu_str = 'edit.php?post_type=' . $type;
}
foreach( $menu as $menu_key => $menu_data ) {
if( $menu_str != $menu_data[2] )
continue;
$menu[$menu_key][0] .= " <span class='update-plugins count-$pending_count'><span class='plugin-count'>" . number_format_i18n($pending_count) . '</span></span>';
}
}
return $menu;
}
add_filter('add_menu_classes', 'show_pending_number');