要动态添加菜单项,可以使用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\' );
}