Opened 10 years ago
Last modified 7 years ago
#28140 new defect (bug)
Position for first menu item on menu is 0 when it should be 1
Reported by: | danielbachhuber | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Menus | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
When the first menu item is added to a menu and no position is explicitly set, its position is set as 0 instead of 1.
Offending line: https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/nav-menu.php#L344
Attachments (3)
Change History (10)
#3
@
10 years ago
- Keywords reporter-feedback added
When the first menu item is added to a menu and no position is explicitly set, its position is set as 0 instead of 1.
Could you describe the issue with more details, and why it should be 1?
#4
@
10 years ago
- Keywords reporter-feedback removed
Because 0 is an invalid menu position and it breaks things?
The historical context is in that pull request. I don't recall the exact specifics off the top of my head, but I vaguely remember that the positioning for a menu would be "0,2,3" without the hack (which broke other things).
#7
@
7 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
The position of menu items is stored as 0,2,3, ...
in the menu_order
field of the wp_posts
table, as @danielbachhuber mentioned.
We note that it's not possible to test this behavior directly with wp_get_nav_menu_items()
, because the position (menu_order
) is adjusted from 0,2,3,...
to 1,2,3,...
with this part of that function:
$i = 1; foreach ( $items as $k => $item ) { $items[$k]->{$args['output_key']} = $i++; }
where $args['output_key']
is menu_order
.
See here:
https://core.trac.wordpress.org/browser/tags/4.8.2/src/wp-includes/nav-menu.php#L657-L660
In 28140.3.patch we
- Adjust the patch in 28140.2.diff with
1 == (int) $args['menu-item-position']
instead of0 == (int) $args['menu-item-position']
- Add a test to check if the position (menu_order) of two newly added menu items is 1 and then 2.
- Add a test to check if the position (menu_order) from
wp_get_nav_menu_items()
is 1 and 2.
From https://github.com/wp-cli/wp-cli/pull/1182