Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue on CPT Custom Menu Position in WP

I have Two CPT as "Dress" and "Casual" and I would like to position them close to each other exactly after the "Dashboard" in the Menu.

I have this code in my custom post type for both:

'menu_position'       => 2

how ever the result is displaying as: enter image description here

the "Post" positions after the "Dress"! can you please let me know why this is happening and how I can keep all CPT after each other before the "Post".(I have 5 Custom Post Type)

Thanks

like image 466
user1760110 Avatar asked Nov 01 '22 06:11

user1760110


1 Answers

If Wordpress already has a menu item in the menu_position that you declare then it is moved to the next free position. Your issue is that Dashboard is in position 2 and then a separator is in position 4, so the second of your Post Types is moved to the next free position which is 6 (Posts is in position 5).

Note that Menu items are added in the order in which they are registered, so if they have the same menu_position, Post Types registered first will appear closer to the top of the menu.

However, you can move the default WP menu items if you so wish, thus achieving your desired display.

If you specify your menu_position as 6 and 7 for your custom Post Types, you can add this to move the separator to position 8 and Posts to position 9 -

add_action('admin_head', 'my_edit_admin_menu');
function my_edit_admin_menu(){

    global $menu;

    $menu[8] = $menu[4];  // Copy 'separator1' to a new menu location
    unset($menu[4]);      // Unset separator1 (from original position)

    $menu[9] = $menu[5];  // Copy 'Posts' to a new menu location
    unset($menu[5]);      // Unset Posts (from original position)

    ksort($menu);         // Sort the menu

}

Note - the action admin_head is called after your custom Post Types are registered (using the init action). Because of this, you must ensure that your given menu_position values do clash with those of default menu items. If there is a clash, WP will move them before you get a chance to reorder the default items.

See the docs for menu_position in the Arguments section of the Function Reference for register_post_type() for the current WP default menu positions (although I believe that it is currently out of date for version 3.8). If you are ever unsure how the menu is organised, add this to the above code to output the full menu in the admin area -

echo '<pre>WP Admin Menu: '; print_r($menu); echo '</pre>';

FYI - I notice that you are using an image as the icon for you custom Post Types. As of WP 3.8, these were removed from the default admin dashboard in favour of DashIcons.

To make your Post Types look as pretty as the rest simply use the menu_icon argument, picking any of the icons from this page. To get the relevant name, click one and then look at the top of the page; the first icon is called dashicons-menu, for example.

like image 101
David Gard Avatar answered Nov 15 '22 02:11

David Gard