我想在管理中的导航项旁边添加一个漂亮的小通知气泡。但我不想每次都触发一个post查询来显示小气泡中的值,从而减慢整个管理的速度。希望将这一整数值加载到临时缓存中,以便在菜单中显示。
开发一个注册自定义帖子类型的自定义插件。这会自动在admin中创建导航项,我想在其中添加气泡。气泡中的值将从一个简单的查询中提取,该查询显示有多少帖子(这个CPT)被分配给了自定义分类法。例如,如果帖子已分配给自定义术语“待定审核”,则添加到气泡计数。
虽然我不知道如何过渡到使用瞬态缓存来存储和检索数据,但这个函数很有魅力。。。。如有任何建议,将不胜感激。谢谢
add_action( \'admin_menu\', \'add_cpt_menu_bubble\' );
function add_cpt_menu_bubble() {
global $menu;
$count_posts = 0;
// count the number of posts to show in bubble
$args = array(
\'order\' => \'DESC\',
\'posts_per_page\' => \'-1\',
\'post_type\' => \'custom_post_type_name\',
\'custom_tax_name\' => \'pending-review\'
);
// The Query
query_posts( $args );
if (have_posts()) :
while ( have_posts() ) : the_post();
$count_posts++;
endwhile;
else:
endif;
wp_reset_query();
// only display the number of pending posts over a certain amount
if ( $count_posts > 5 ) {
foreach ( $menu as $key => $value ) {
if ( $menu[$key][2] == \'edit.php?post_type=custom_post_type_name\' ) {
$menu[$key][0] .= \' <span class="update-plugins count-2"><span class="update-count">\' . $count_posts . \'</span></span>\';
return;
}
}
}
} // EOF