-
-
Save daggerhart/c17bdc51662be5a588c9 to your computer and use it in GitHub Desktop.
fix_menu_orders is causing ordering issues in WordPress 4.7
@chetzof what issues exactly are they?
The issue is that '_sort_nav_menu_items' is depreciated by Wordpress since 4.7.
For solving this issue, update the function fix_menu_orders.
/**
* Menu items with the same menu_order property cause a conflict. This
* method attempts to provide each menu item with its own unique order value.
*
* @param $items
*
* @return mixed
*/
private function fix_menu_orders( $items ) {
for( $i = 0; $i < count( $items ); $i++ ){
$items[ $i ]->menu_order = $i;
}
wp_list_sort($items, $items[ $i ]->menu_order, 'ASC');
return $items;
}
@damienbuchs that didn't work for me; throws a lot of undefined offsets and non-object errors.
This worked for me:
/**
* Menu items with the same menu_order property cause a conflict. This
* method attempts to provide each menu item with its own unique order value.
*
* @param $items
*
* @return mixed
*/
private function fix_menu_orders( $items ){
$items = wp_list_sort( $items, 'menu_order' );
for( $i = 0; $i < count( $items ); $i++ ){
$items[ $i ]->menu_order = $i;
}
return $items;
}
Very awesome class you build here.
I had a need to programmatically add the newly created custom post type items, to the default nav menu.
So that i have a parent child relation, foreach of my cpt's, withone having the user manually adding to the menu.
I only problem i face now, is the class which is added to the new sub-menu points.
Normally when a posttype item is added manually to the wp_nav menu, the class added will match the posttype its in.
like so : menu-item-type-projects menu-item-object-projects
But when i use this awesome class, the classes is added to the li without the posttype in the end,
like so : menu-item-type- menu-item-object-
Is there a way to hook into the function which adds the classes, in order to add the correct posttype extension to the menu item.
@codepuncher - thanks for the updated method, I've added it to the gist.
@vandelio - I've added a new entry point for adding Posts and Taxonomy terms to the gist, and updated the example with its usage. See custom_menu_items::add_object()
When adding posts or taxonomy terms with add_object
, this notice will happen for the currently active menu item:
Notice: Undefined property: stdClass::$post_parent in .../wp-includes/nav-menu-template.php on line 415
To avoid this, we must add a post_parent
property to the $item_obj
object on the make_item_obj
method.
From the tests I've done adding both posts and taxonomy terms via the regular Appearance > Menus tool at wp-admin, I've come to the conclusion that post_parent
is the actual post_parent
for posts and the term parent
for taxonomy terms.
So this is what needs to be done:
-
At
add_object
: Addpost_parent
as$object->post_parent
to the menu_item when$object_type == 'post'
-
At
add_object
: Add tt.parent to the SQL query atadd_object
when$object_type == 'term'
-
At
add_object
: Addpost_parent
as$object->parent
to the menu_item when$object_type == 'term'
-
At
make_item_obj
: Add$item_obj->post_parent
as!empty( $item['post_parent'] ) ? $item['post_parent'] : ''
You can find the full class with the changes at https://gist.github.com/webdados/e069ce33947a0e981aede33c79a4d837
Thanks for digging into that @webdados!
Those changes make great sense. I've updated this gist to reflect the changes. Very much appreciated!
You're welcome @daggerhart
I added "classes" support here: https://gist.github.com/webdados/d7a31f1363ef65d8a220e5ec87c1f1e8
Moved to this repo. PRs welcome! https://github.com/daggerhart/wp-custom-menu-items
Nice!
Moved to a repo: wp-custom-menu-items
Related blog post: Dynamically add items to WordPress menus
Example usage: