在管理菜单/边栏中添加菜单链接(指向WordPress页面)

时间:2014-07-27 作者:codeview

我试图在管理侧边栏中添加一个链接,链接到我在页面部分创建的一个页面。例如,我创建了一个关于页面。因此,在侧栏中,我希望有一个指向加载此链接的“关于”的链接:post.php?post=7&action=edit

为什么?我之所以这么做,是因为我在一个有4种自定义帖子类型和3个页面的网站上工作。自定义帖子类型有管理菜单链接,因此我认为如果这3个页面在侧边栏中也有直接链接,而不是需要单击页面,然后查看3个页面的列表,然后单击“编辑”(然后我会从管理菜单中隐藏页面部分链接),这将是更好的用户体验。

我正沿着这条路前进,但它似乎不是我想要/需要的(我无法将它链接到我想要的地方):http://codex.wordpress.org/Administration_Menus#Inserting_the_Pages

2 个回复
最合适的回答,由SO网友:Bryan Willis 整理而成

要动态添加菜单项,可以使用WP\\u Query,特别是get\\u posts或get\\u pages。获取页面更加一致。下面是一个将所有页面添加到页面管理菜单的示例。您可以在下面的$参数中更改参数以排除、包括、orderby等。要更改为自定义帖子类型,只需将$custom变量更改为您的帖子类型名称。

/*-----------------------------------------------------------------------------------*/
/* All Pages Dropdown List */
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( \'admin_menu_links_to_all_edit_post_type_custom\' ) ) {
function admin_menu_links_to_all_edit_post_type_custom() {
    if ( !is_admin() ) // Only Run if On Admin Pages
        return;

     $custom = \'page\';  // Change this to your custom post type slug ( So for "http://www.example.com/wp-admin/edit.php?post_type=recipes" you would change this to \'recipes\'  )



      // Full List of Paramaters - http://codex.wordpress.org/Template_Tags/get_posts
      $args = array(
          \'orderby\'          => \'modified\', //Orderr by date , title , modified, etc
          \'order\'            => \'DESC\', // Show most recently edited on top
          \'post_type\'        => $custom, // Post Type Slug
          \'numberposts\'      => -1,  // Number of Posts to Show (Use -1 to Show All)
          \'post_status\'      => array(\'publish\', \'pending\', \'draft\', \'future\', \'private\', \'inherit\'),
      );
      $types = get_posts( $args ); // Get All Pages
      foreach ($types as $post_type) {
      add_submenu_page( // add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
          \'edit.php?post_type=\'.$custom
        , esc_attr(ucwords($post_type->post_title)) // Get title, remove bad characters, and uppercase it
        , esc_attr(ucwords($post_type->post_title)) // Get title, remove bad characters, and uppercase it
        , \'edit_posts\' // Require Edit Post/Page/Custom Capability
        , \'post.php?post=\' . $post_type->ID . \'&action=edit\' // Get the page link by its id
        , \'\' // No function callback
      );    
      }
      wp_reset_postdata();
}
add_action(\'admin_menu\', \'admin_menu_links_to_all_edit_post_type_custom\');
}

if ( !function_exists( \'admin_menu_links_to_all_edit_post_type_custom_css\' ) ) {
    function admin_menu_links_to_all_edit_post_type_custom_css() {
?>
<style type="text/css">
ul#adminmenu li.wp-has-submenu > ul.wp-submenu.wp-submenu-wrap {
max-height: 700px;
overflow-x: scroll;
}
</style>
<?php
   }
    add_action(\'admin_head\', \'admin_menu_links_to_all_edit_post_type_custom_css\');
}
Or to just add individually:

/*-----------------------------------------------------------------------------------*/
/* Single URL Submenu */
/*-----------------------------------------------------------------------------------*/

if ( !function_exists( \'single_submenu_dropdown_link_example\' ) ) {
function single_submenu_dropdown_link_example() {
    global $submenu;
    $link_to_add = \'post.php?post=7&action=edit\';
    // change edit.php to the top level menu you want to add it to 
    $submenu[\'edit.php\'][] = array(\'About\', \'edit_posts\', $link_to_add);
}
add_action(\'admin_menu\', \'single_submenu_dropdown_link_example\');
}

This will add toplevel admin menus to your other pages:

/*-----------------------------------------------------------------------------------*/
/* Toplevel Page Menus */
/*-----------------------------------------------------------------------------------*/

if ( ! function_exists( \'toplevel_admin_menu_pages\' ) ) {
function toplevel_admin_menu_pages(){
if ( !current_user_can(\'administrator\') ) {  // If the user is not the administrator remove and add new menus
    remove_menu_page( \'index.php\' );                  //Dashboard
    remove_menu_page( \'edit.php\' );                   //Posts
    remove_menu_page( \'upload.php\' );                 //Media
    remove_menu_page( \'edit.php?post_type=page\' );    //Pages
    remove_menu_page( \'edit-comments.php\' );          //Comments
    remove_menu_page( \'themes.php\' );                 //Appearance
    remove_menu_page( \'plugins.php\' );                //Plugins
    remove_menu_page( \'users.php\' );                  //Users
    remove_menu_page( \'tools.php\' );                  //Tools
    remove_menu_page( \'options-general.php\' );        //Settings
    add_menu_page( \'Home\', \'Home\', \'edit_posts\', \'post.php?post=39&action=edit\', \'\', \'dashicons-admin-home\', 6 );
    add_menu_page( \'About\', \'About\', \'edit_posts\', \'post.php?post=15&action=edit\', \'\', \'dashicons-editor-help\', 7 );
    add_menu_page( \'Services\', \'Services\', \'edit_posts\', \'post.php?post=24&action=edit\', \'\', \'dashicons-admin-tools\', 8 );
    }
  }
add_action( \'admin_menu\', \'toplevel_admin_menu_pages\' );
}

SO网友:Bryan Willis
if ( !function_exists( \'wp_toolbar_frontend_admin_menu_links_extras\' ) ) {

function wp_toolbar_frontend_admin_menu_links_extras($wp_admin_bar) {   
    if ( is_admin() || !is_admin_bar_showing() )
          return;

   if ( !current_user_can(\'edit_pages\') ) 
        return;

    $wp_admin_bar->add_node(array(
        \'id\' => \'homepage\',
        \'title\' => \'Home\',
        \'href\' => admin_url() . \'post.php?post=39&action=edit\' ,
        \'parent\' => \'site-name\',
        \'meta\' => array(
            \'class\' => \'homepage\'
        )
    ));

    $wp_admin_bar->add_node(array(
        \'id\' => \'aboutus\',
        \'title\' => \'About\',
        \'href\' => admin_url() . \'post.php?post=36&action=edit\' ,
        \'parent\' => \'site-name\',
        \'meta\' => array(
            \'class\' => \'aboutus\'
        )
    ));

    $wp_admin_bar->add_node(array(
        \'id\' => \'services\',
        \'title\' => \'Services\',
        \'href\' => admin_url(). \'post.php?post=32&action=edit\' ,
        \'parent\' => \'site-name\',
        \'meta\' => array(
            \'class\' => \'services\'
        )
    ));
}

    add_action(\'admin_bar_menu\', \'wp_toolbar_frontend_admin_menu_links_extras\', 99);

}
结束

相关推荐

WP_DROPDOWN_PAGES()为POST类型返回空字符串

wp_dropdown_pages() 当用于任何职位类型时page 返回空字符串。Codex says post类型可以作为键传递,函数也可以使用get_pages() 这需要post_type 钥匙所以,我不明白为什么它对我不起作用。 $args = array( \'echo\' => 1, \'name\' => \'my_posts\', \'post_type\' => \'post\',