将post-new.php?post_cat=catslug添加到管理菜单

时间:2013-04-27 作者:David Lok

我使使用post new成为可能。php?post\\u cat=catslug在wordpress admin中使用以下答案创建带有预填充类别的新帖子:How to add category to: 'wp-admin/post-new.php'?

现在,我想在“Posts”->“Add new”管理菜单项下创建多个此格式的菜单项。因此,菜单将是:

职位

所有帖子新增。php?post\\u cat=profile添加新消息->发布新消息。php?post\\u cat=新闻添加新音高->发布新音高。php?post\\u cat=俯仰分类等。我能够在功能中使用此代码在菜单底部添加一个项目。php

add_action( \'admin_menu\' , \'admin_menu_new_items\' );
function admin_menu_new_items() {
    global $submenu;
    $submenu[\'edit.php\'][500] = array( \'Add new profile\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitches\' ); 
}  
问题:

这是最好的方法吗?或者我是否需要使用此处描述的函数之一http://codex.wordpress.org/Administration_Menus. 我试了一些,但没能让它按我想要的方式工作

  • 如何在“添加新内容”(而不是在“标签”下方)下添加菜单项

  • 1 个回复
    最合适的回答,由SO网友:Charles Clarkson 整理而成

    要查看当前阵列键,请尝试以下操作:

    add_action( \'admin_menu\' , \'admin_menu_new_items\', 1 );
    function admin_menu_new_items() {
        global $submenu;
        wp_die( \'<pre>\' . var_export( $submenu[\'edit.php\'], true ) . \'</pre>\' );
    }
    
    我明白了:

    array (
      5 => 
      array (
        0 => \'All Posts\',
        1 => \'edit_posts\',
        2 => \'edit.php\',
      ),
      10 => 
      array (
        0 => \'Add New\',
        1 => \'edit_posts\',
        2 => \'post-new.php\',
      ),
      15 => 
      array (
        0 => \'Categories\',
        1 => \'manage_categories\',
        2 => \'edit-tags.php?taxonomy=category\',
      ),
      16 => 
      array (
        0 => \'Tags\',
        1 => \'manage_categories\',
        2 => \'edit-tags.php?taxonomy=post_tag\',
      ),
    )
    
    索引“10”是添加新项。要添加新的子菜单项,请使用索引11、12和13:

        $submenu[\'edit.php\'][11] = array( \'Add New Profile\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitches\' );
        $submenu[\'edit.php\'][12] = array( \'Add New News\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=news\' );
        $submenu[\'edit.php\'][13] = array( \'Add New Pitch\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitch\' );
    
    当WordPress将子菜单项添加到它处理的仪表板时$submenu作为关联数组。添加到edit.php无论使用的键是什么,数组都将添加到数组的末尾。使用ksort() 订购钥匙。

        ksort( $submenu[\'edit.php\'], SORT_NUMERIC );
    
    总而言之:

    add_action( \'admin_menu\' , \'admin_menu_new_items\', 1 );
    function admin_menu_new_items() {
        global $submenu;
    
        $submenu[\'edit.php\'][11] = array( \'Add New Profile\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitches\' );
        $submenu[\'edit.php\'][12] = array( \'Add New News\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=news\' );
        $submenu[\'edit.php\'][13] = array( \'Add New Pitch\', \'manage_options\' , \'/wp-admin/post-new.php?post_cat=pitch\' );
    
        // WordPress treats $submenu as an associative array and does not sort it first.
        // We have to sort keys into the order we want them to show up.
        ksort( $submenu[\'edit.php\'], SORT_NUMERIC );
    }
    

    结束

    相关推荐