i want insert a dropdown menu with list of menus, that i’ve created in nav-menu.php, to post-admin…
For example, when i’m creating a post, i want insert a menu “Paul” in a post (only a post that i want) selecting a menu at right sidebar of Admin-Post.
I’ve copied the rows:
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'personal-menu' => __( 'Personal Menu' )
)
);
}
in a post.fields.php that i’ve created, don’t work. 🙁
Can you help me in that fantastic problem? 🙁
ps: Sorry for my bad english
Francesco
To see how such a list can be created look at the code in
wp-admin/includes/nav-menu.php
. Unfortunately, it is hard coded there, so you have to re-create the code.First, letâs create two menus:
We can get these menus in PHP with
wp_get_nav_menus()
(a wrapper forget_terms()
):Output:
Now we build the selector function:
Calling this function â¦
⦠will give us a nice, simple select element with
'paul'
pre-selected:Now we need a meta box to offer the selector to our post authors. To keep this short, I just use a child class of my Basic Meta Box. Basically, you call the function in the meta box and save the menu slug as post meta.
Then you can access the selected nav menu slug in your theme per:
Another way to get the full nav menu in your theme:
Because I like your idea very much Iâve added a theme helper function and mangled everything into a plugin. Get it on GitHub: T5 Nav Menu Per Post.
Oh, and welcome to WordPress Stack Exchange. Donât worry about your English as long as you ask â and answer â interesting questions. 🙂
Update
To use the plugin with a custom post type like your
portfolio
just install the plugin, activate it and add the following code to the file where you register the custom post type:Your code still has many problems: global variables, missing prefixes, weird indentation. Recommended reading: Coding Standards and prefix everything.
Had the same problem regarding the custom post_type not saving the data. It was due to it failing
save_allowed()
inbasic_meta_box.php
.current_user_can('edit_'.$_POST['post_type'],$post_id)
which with a custom post type resolves tocurrent_user_can('edit_portfolio',$post_id)
– which fails in wordpress. need to change to a valid capability likeedit_others_pages
oredit_pages
.