wordpress add_menu_page中的重复标签

ztmd8pv5  于 2023-06-21  发布在  WordPress
关注(0)|答案(6)|浏览(126)

我创建了一个主题定制功能。

function create_theme_option() {

        add_menu_page(  'Manage Options', //Page Title 

                        'Theme Option', //WP Administrator Menu Title

                        'manage_options', //

                        'theme-options', //Link to a page to your Administration Area

                        'deploy_theme_options', //Function Name

                        get_template_directory_uri() . '/Plugins/Background Changer/images/icons/icon.png',//Menu Icon 

                        99);

        add_submenu_page("theme-options", "Theme Settings", "Theme Settings", 1, "theme-settings", "theme_settings");

        add_submenu_page("theme-options", "Manage Header", "Manage Header", 1, "manage-header", "manage_header");

        add_submenu_page("theme-options", "Social Media", "Social Media Links", 1, "social-media", "social_media");

        add_submenu_page("theme-options", "Catalog Manager", "Catalog Manager", 1, "catalog-manager", "catalog_manager");

    }

但我注意到,在标签“主题选项”之后,有另一个文本出现在它旁边作为“主题选项”。检查下面的图片:

我该怎么解决这个问题?救命啊!

huus2vyu

huus2vyu1#

@Basharat非常接近。以下是我在插件中使用的更干净的方法:

add_menu_page(
    '',                     // No need to have this
    'My Plugin',            // Menu Label
    'manage_options',
    'my_plugin_settings',   // (*) Shared slug
    'your_function',
    plugins_url('myplugin/images/icon.png'),
    81
);

add_submenu_page(
    'my_plugin_settings',   // (*) Shared slug
    'My Plugin Settings',   // Subpage Title
    'Settings',             // Submenu Label
    'manage_options',
    'my_plugin_settings',   // (*) Shared slug
    'your_function'
);

**来源:**咖啡,试错;)

ac1kyiln

ac1kyiln2#

要解决此问题,您可以使用remove_submenu_page,http://codex.wordpress.org/Function_Reference/remove_submenu_page
示例:
remove_submenu_page('my-menu-slug','my-menu-slug')

brccelvz

brccelvz3#

请使用以下方法删除子菜单中菜单的重复条目。

<?php 
/* Add top level menu */
add_menu_page(
    'MyTheme', 
    'MyTheme Menu Label',
    'edit_themes', 
    'theme_admin',        // menu slug
    'functions.php',        // function
    get_bloginfo('template_directory') .'/img/favicon.png',
    31
);

/* remove duplicate menu hack */
add_submenu_page(
    'theme_admin',        // parent slug, same as above menu slug
    '',        // empty page title
    '',        // empty menu title
    'edit_themes',        // same capability as above
    'theme_admin',        // same menu slug as parent slug
    'functions.php',        // same function as above
}
?>

这不是太干净,但afaik唯一的方法来隐藏重复的子菜单。

参考:https://wordpress.stackexchange.com/questions/52675/how-to-remove-duplicate-link-from-add-menu-page

wh6knrhe

wh6knrhe5#

我找到的唯一有效的方法是将add_menu_page $capabilities设置为unknown,这样就可以隐藏页面或子菜单,但菜单仍然会显示在左侧。

hgb9j2n6

hgb9j2n66#

要删除重复的菜单,只需明确定义它的显示方式(如果使用其他名称)或删除它。让我们看看下面的两个案例。
案例1 -您希望显示第一个插件页面,但使用另一个名称:

<?php
    
add_action('admin_menu', 'register_my_plugin');

function register_my_plugin()
{
    add_menu_page(
        'Welcome my plugin', // page title (whatever)
        'My Plugin Name', // menu title (whatever)
        'manage_options', // necessary capacity
        'page-1', // page slug
        'fn_page_1', // your function to load page 1 resources
        'dashicons-lightbulb' //a cute icon from developer.wordpress.org/resource/
    );
    
    add_submenu_page( //Identical to the menu except for the label
        'page-1',  // parent slug
        'Welcome', // page title (whatever)
        'Welcome',  // Label do submenu (whatever) --> (replaces duplicate My Plugin Name)
        'manage_options', // necessary capacity
        'page-1',   // your function to load page 1 resources
        'fn_page_1', // function to load page 1 resources again
    );
    
    add_submenu_page(
        'page-1',  // parent slug
        'Next Page', // page title (whatever)
        'Next Page',  // Submenu label  (whatever) 
        'manage_options', // necessary capacity
        'page-2',   // page slug
        'fn_page_2', // your function to load page 2 resources
    );
} //close register_my_plugin

案例2 -您想从子菜单中删除第一个插件页面:
包含所有代码后,只需在关闭函数register_my_plugin()之前添加以下内容

remove_submenu_page(
        'page-1','page-1'
    );
} //close register_my_plugin

相关问题