Thread Starter
jaroat
(@jaroat)
Hi –
Seems to be a problem if a nav menu item doesn’t refer to a wp_posts entry.
In my case, the menu item was linked to a category (resulting in a link to the category overview page) so the object_id doesn’t refer to a wp_posts row but on a wp_term instead. get_post fails in that case and returns null.
A possible solution would be to extend the format_menu_item function a little bit so it differentiates between the possible menu-item-types (taxonomy, custom, post_type, etc.).
Best from Salzburg!
– Johannes
Thread Starter
jaroat
(@jaroat)
Something like this should do it:
switch ( $item[ 'type' ] ) {
case 'post_type':
$post = get_post( $item['object_id'] );
$menu_item = array(
'id' => abs( $item['ID'] ),
'order' => (int) $item['menu_order'],
'parent' => abs( $item['menu_item_parent'] ),
'title' => $item['title'],
'url' => $item['url'],
'attr' => $item['attr_title'],
'target' => $item['target'],
'classes' => implode( ' ', $item['classes'] ),
'xfn' => $item['xfn'],
'description' => $item['description'],
'object_id' => abs( $item['object_id'] ),
'object' => $item['object'],
'object_slug' => $post !== null ? $post->post_name : '',
'type' => $item['type'],
'type_label' => $item['type_label'],
);
break;
default:
$menu_item = array(
'id' => abs( $item['ID'] ),
'order' => (int) $item['menu_order'],
'parent' => abs( $item['menu_item_parent'] ),
'title' => $item['title'],
'url' => $item['url'],
'attr' => $item['attr_title'],
'target' => $item['target'],
'classes' => implode( ' ', $item['classes'] ),
'xfn' => $item['xfn'],
'description' => $item['description'],
'object_id' => abs( $item['object_id'] ),
'object' => $item['object'],
'object_slug' => null,
'type' => $item['type'],
'type_label' => $item['type_label'],
);
break;
}
-
This reply was modified 5 years, 8 months ago by
jaroat.