Admin option sidebar count

时间:2013-05-28 作者:Jens Törnell

我需要在管理侧边栏的选项菜单项中添加一个计数。这是如何做到的?

I found a similar post:

Modifying admin sidebar contents to show pending posts indicator

1 个回复
SO网友:Bainternet

如果你是说像这样enter image description here

下面是一个方便的类,可以快速完成:

/**
* add_num_to_admin_menu
* @author Ohad Raz <[email protected]>
*/
class add_num_to_admin_menu
{
    public $menus = array();
    /**
     * Class constructor
     * @author Ohad Raz <[email protected]>
     * 
     * @param array $args menu => number format
     */
    function __construct($args = array()){
        if (!is_admin())return;
        $this->menus = $args;
        add_filter( \'add_menu_classes\', array($this,\'_Add_num\'));
    }
    /**
     * the actual function that adds the number to the menu
     * 
     * @author Ohad Raz <[email protected]>
     * @param  array $menu admin menu
     * @return array
     */
    function _Add_num($menu){
        //var_dump($menu);
        foreach ((array)$this->menus as $name => $number) {
            // loop through $menu items, find match, add indicator
            foreach( $menu as $menu_key => $menu_data ) {
                if( $name != $menu_data[2] )
                    continue;
                $menu[$menu_key][0] .= " <span class=\'update-plugins count-$number\'><span class=\'plugin-count\'>" . number_format_i18n($number) . \'</span></span>\';
            }
        }

        return $menu;
    }
    /**
     * Add a number to a menu
     * 
     * @author Ohad Raz <[email protected]>
     * @param string $menu  menu to add the number to
     * @param int $number number to add
     */
    function addMenuNum($menu,$number){
        $this->menus[$menu] = $number;
    }
}

Usage:

//create an instance of the class
$obj = new add_num_to_admin_menu();
//add a number to a menu node
$obj->addMenuNum(\'edit.php\',7);
//repeat as many times as needed
$obj->addMenuNum(\'upload.php\',20);
$obj->addMenuNum(\'link-manager.php\',15);
$obj->addMenuNum(\'users.php\',10);

结束

相关推荐

Register multiple sidebars

我正在注册我的边栏,如下所示:$sidebars = array ( \'sidebar-10\' => \'Main Sidebar\', \'sidebar-11\' => \'Homepage Area One\', \'sidebar-12\' => \'Homepage Area Two\', \'sidebar-1