快速而肮脏的答案是这样的:
function custom_admin_menu_links() {
$user = wp_get_current_user();
if ( $user->has_cap( \'manage_options\' ) ) {
$type = \'book\';
$page = get_page_by_title(\'home\',\'object\',$type);
if (!empty($page)) {
add_menu_page( \'Home\', \'Home\', \'edit_posts\', \'post.php?post=\'.$page->ID.\'&action=edit\', \'\', \'dashicons-admin-home\', 1);
}
$page = get_page_by_title(\'About\',\'object\',$type);
if (!empty($page)) {
add_menu_page( \'About\', \'About\', \'edit_posts\', \'post.php?post=8&action=edit\', \'\', \'dashicons-id\', 2);
}
$page = get_page_by_title(\'Contact\',\'object\',$type);
if (!empty($page)) {
add_menu_page( \'Contact\', \'Contact\', \'edit_posts\', \'post.php?post=10&action=edit\', \'\', \'dashicons-phone\', 3);
}
}
}
add_action( \'admin_menu\', \'custom_admin_menu_links\' );
但是您在每次加载管理页面时都会运行大量查询。您真的应该找到一种缓存结果的方法。类似于:
function cache_custom_admin_menu_links($post_ID, $post) {
$links = get_option(\'my_menu_links\');
$keys = array(
\'home\',
\'about\',
\'contact\'
);
if (in_array($post->post_name,$keys)) {
$links[$post->post_name] = $post_ID;
update_option(\'my_menu_links\',$links);
}
}
add_action(\'save_post\',\'cache_custom_admin_menu_links\',10,2);
function custom_admin_menu_links() {
$user = wp_get_current_user();
if ( $user->has_cap( \'manage_options\' ) ) {
$type = \'book\';
$links = get_option(\'my_menu_links\');
$i = 1;
foreach ($links as $k=>$v) {
add_menu_page( $k, ucfirst($k), \'edit_posts\', \'post.php?post=\'.$v.\'&action=edit\', \'\', \'dashicons-phone\', $i);
$i++;
}
}
}
add_action( \'admin_menu\', \'custom_admin_menu_links\' );
然后你只需要重新保存你的帖子,而不是编辑代码。我相信,如果你需要的话,即使这样也可以实现自动化。