在保持菜单展开的同时隐藏管理页面

时间:2017-03-17 作者:Greeso

我正在创建一个自定义管理部分。我有以下代码:

// Top level menu
add_menu_page(\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);

// Adding this function to make the first submenu have a different name than the main menu
// Details: https://wordpress.stackexchange.com/questions/66498/add-menu-page-with-different-name-for-first-submenu-item
add_submenu_page(\'Books\', \'Books\', \'All Books\', \'publish_posts\', \'books\', \'render_books_page\');

// The Add Book menu page
add_submenu_page(\'Books\', \'Add New Book\', \'Add Book\', \'publish_posts\', \'add-book\', \'render_add_book_page\');

// The Edit Book menu page (this page is hidden from the menu, and accessed via the All Books page only)
add_submenu_page(null, \'Edit Book\', \'Edit Book\', \'publish_posts\', \'edit-book\', \'render_edit_book_page\');
正如您在最后一行代码中所注意到的add_submenu_page() 设置为null. 这是为了确保Edit Book 页面已隐藏(more details about this here). 访问Edit Book 页面通过主菜单从所有书籍列表中完成。

问题是,当我去Edit Book 页面中,左侧的管理菜单将折叠(另一方面,默认的WordPress行为如下:如果您转到Edit Post 第页,或Edit Page 第页,两个PostsPages 菜单保持展开状态,用于各自的编辑页面)。在我的情况下,菜单会折叠。

当我转到Edit Book 佩奇,以类似于WordPress的方式行事?

谢谢

3 个回复
最合适的回答,由SO网友:Greeso 整理而成

解决方案基于@Ian提供的想法。谢谢

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page (\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);

    // Adding this function to make the first submenu have a different name than the main menu
    add_submenu_page(\'books\', \'Books\', \'All Books\', \'publish_posts\', \'books\', \'render_books_page\' );

    if ((isset($_GET[\'page\'])) && ($_GET[\'page\'] === \'edit-book\')) {
        // The Edit Book menu page and display it as the All Books page
        add_submenu_page(\'books\', \'Edit Book\', \'All Books\', \'publish_posts\', \'edit-book\', \'render_edit_book_page\' );
    }

    // The add-book menu page
    add_submenu_page(\'books\', \'Add New Book\', \'Add New\', \'publish_posts\', \'add-book\', \'render_add_book_page\' );
}
我们必须隐藏第一个菜单项

add_action( \'admin_enqueue_scripts\', function () {

    if ((isset($_GET[\'page\'])) && ($_GET[\'page\'] === \'edit-book\')) {

        // Load CSS file
        wp_enqueue_style(\'book-edit\', \'path/to/css/menu.css\');

        // Load jQuery
        wp_enqueue_script(\'jquery\');

        // Load 
        wp_enqueue_script(\'book-edit-script\', \'path/to/js/menu.js\');
    }
});
以及menu.css 是:

#toplevel_page_books li.current {
    display: none;
}

#toplevel_page_books li.wp-first-item {
    display: list-item;
}
还有“菜单”的内容。js’是:

jQuery(document).ready(function($) {

    $(\'#toplevel_page_books li.wp-first-item\').addClass(\'current\');

});
<小时>

TL;DR

为了了解所有这些是如何工作的,这里有一个逐步的解释。

Step 1: 我们添加主菜单项(书籍菜单项)以显示书籍列表

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page (\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);
}
Step 2: 我们将添加书本菜单项作为子菜单添加到主书本菜单项

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page (\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);

    // The add-book menu page
    add_submenu_page(\'books\', \'Add New Book\', \'Add New\', \'publish_posts\', \'add-book\', \'render_add_book_page\' );
}
Step 3: 完成上述步骤2后,将添加books菜单项,左侧的菜单列表如下所示:

Books              <---------- This is the main top level menu names
  Books            <---------- This is the first sub-menu
  Add New          <---------- This is the second sub-menu
然而,我们应该解决这个问题。预期列表应如下所示

Books              <---------- This is the main top level menu names
  All Books        <---------- This is the first sub-menu
  Add New          <---------- This is the second sub-menu
为此,我们必须修改代码如下:

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page (\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);

    // Adding this function to make the first submenu have a different name than the main menu
    add_submenu_page(\'books\', \'Books\', \'All Books\', \'publish_posts\', \'books\', \'render_books_page\' );

    // The add-book menu page
    add_submenu_page(\'books\', \'Add New Book\', \'Add New\', \'publish_posts\', \'add-book\', \'render_add_book_page\' );
}
Step 4: 接下来,我们应该添加一个子菜单来编辑书籍(编辑书籍菜单项)。添加子菜单后,当我们在编辑图书页面时,左侧的菜单应如下所示:

Books
  All Books        <---------- When we are in the \'edit-book\' page, this menu item is selected and is highlighted (typically white in color), and also clicking on "All Books" would return us back to the "All Books" page.
  Add New
我首先尝试的解决方案是我在原始问题中发布的内容,但并不完全有效。因此,根据与@Ian的讨论并查看他提出的解决方案,我得出了以下结论:

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page (\'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17);

    // Adding this function to make the first submenu have a different name than the main menu
    add_submenu_page(\'books\', \'Books\', \'All Books\', \'publish_posts\', \'books\', \'render_books_page\' );

    // If we are in the \'edit-book\' page, then display the \'edit-book\' submenu, otherwise, display the regular \'books\' menu
    if ((isset($_GET[\'page\'])) && ($_GET[\'page\'] === \'edit-book\')) {
        // Display the \'edit-book\' menu page and display it as the \'all-books\' page
        // Notice that the slug is \'edit-book\', but the display name is \'All Books\'
        add_submenu_page(\'books\', \'Edit Book\', \'All Books\', \'publish_posts\', \'edit-book\', \'render_edit_book_page\' );
    }

    // The add-book menu page
    add_submenu_page(\'books\', \'Add New Book\', \'Add New\', \'publish_posts\', \'add-book\', \'render_add_book_page\' );
}
现在,如果单击“books”(书本)菜单项或“add book”(添加书本)菜单项,则一切正常。但是,如果我们试图编辑现有书籍,则会显示以下菜单列表

Books
  All Books        <---------- This is the first sub-menu (due to the first submenu call)
  All Books        <---------- This is the \'edit-book\' page (HIGHLIGHTED)
  Add New
Step 5: 现在我们注意到:单击第一个子菜单,将呈现“所有书籍”页面,单击第二个子菜单将呈现“编辑”页面;在我们的例子中,我们希望呈现“所有书籍”页面。因此,我们必须隐藏第二个子菜单并突出显示第一个子菜单。具体操作如下:

add_action( \'admin_enqueue_scripts\', function () {

    if ((isset($_GET[\'page\'])) && ($_GET[\'page\'] === \'edit-book\')) {

        // Load CSS file
        wp_enqueue_style(\'book-edit\', \'path/to/css/menu.css\');

        // Load jQuery
        wp_enqueue_script(\'jquery\');

        // Load 
        wp_enqueue_script(\'book-edit-script\', \'path/to/js/menu.js\');
    }
});
以及menu.css 是:

#toplevel_page_books li.current {
    display: none;
}

#toplevel_page_books li.wp-first-item {
    display: list-item;
}
还有“菜单”的内容。js’是:

jQuery(document).ready(function($) {

    $(\'#toplevel_page_books li.wp-first-item\').addClass(\'current\');

});
现在一切都像一个魔咒。

SO网友:Ian

对于您的特定情况,您需要注册一个菜单,但除非您从另一个页面上的链接单击它,否则不会显示该菜单。您可以添加一个条件检查,以查看您是否在编辑页面上。如果是,则更换null 具有book 根据add_submenu_page() 参数。

add_action( \'admin_menu\', \'add_the_menus\' );
function add_the_menus() {

    // Top level menu
    add_menu_page( \'Books\', \'Books\', \'publish_posts\', \'books\', \'render_books_page\', \'\', 17 );

    // Adding this function to make the first submenu have a different name than the main menu
    add_submenu_page( \'books\', \'Books\', \'All Books\', \'publish_posts\', \'books\', \'render_books_page\' );

    // The Add Book menu page
    add_submenu_page( \'books\', \'Add New Book\', \'Add Book\', \'publish_posts\', \'add-book\', \'render_add_book_page\' );

    // Check the get parameter for page to see if its the page you want to display in the menu only when you\'re on it.
    if ( $_GET[\'page\'] === \'edit-book\' ) {
        // The Edit Book menu page (this page is hidden from the menu, and accessed via the All Books page only)
        add_submenu_page( \'books\', \'Edit Book\', \'Edit Book\', \'publish_posts\', \'edit-book\', \'render_edit_book_page\' );
    } else {
        // The Edit Book menu page (this page is hidden from the menu, and accessed via the All Books page only)
        add_submenu_page( null, \'Edit Book\', \'Edit Book\', \'publish_posts\', \'edit-book\', \'render_edit_book_page\' );
    }
}
附加注释。如果结果表明,即使选中了该菜单项,也需要隐藏该菜单项,则可以将样式排队,以便仅当您在该页面上时才隐藏它。

add_action( \'admin_enqueue_scripts\', function () {
    if ( $_GET[\'page\'] === \'edit-book\' ) {
        wp_enqueue_style( \'book-edit\', get_stylesheet_directory_uri() . \'/assets/css/book-edit.css\' );
    }
} );
书的内容在哪里编辑。css非常简单:

#toplevel_page_books li.current {
    display: none;
}

SO网友:LauGau

实际上,您只需正常注册菜单,父页面的声明如下:

function create_menu_and_submenu_page(){

    // Top level menu
    add_menu_page(
        \'Books\',
        \'Books\',
        \'publish_posts\',
        \'books\',
        \'render_books_page\',
        \'\',
        17
    );

    // Here the other subpages
    // ...

    // Submenu page
    add_submenu_page(
        \'books\',        // Here put the parent slug
        \'Edit Book\',
        \'Edit Book\',
        \'publish_posts\',
        \'edit-book\',
        \'render_edit_book_page\'
    );
}
然后,你只需像这样隐藏它:

// We remove the unecessary links from the left menu
// while keeping the plugin menu selected
add_action( \'admin_head\', function() {
    remove_submenu_page( \'books\', \'edit-book\' );  // \'parent-slug\', \'subpage-slug\'
} );
希望将来能帮助别人。快乐编码:)