Open tomas-javurek opened 7 years ago
Hey this function format_menu_item
just gets the slug for posts/pages but not for terms. I changed the function to get it for terms as well. @metazoa-org I hope you can include this in an update.
public function format_menu_item( $menu_item, $children = false, $menu = array() ) {
$item = (array) $menu_item;
$object_slug = '';
if($item['type'] == 'taxonomy'){
$term = get_term($item['object_id'], $item['object']);
$object_slug = $term->slug;
}else{
$object_slug = get_post( $item['object_id'] )->post_name;
}
$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' => $object_slug,
'type' => $item['type'],
'type_label' => $item['type_label'],
);
if ( $children === true && ! empty( $menu ) ) {
$menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
}
return apply_filters( 'rest_menus_format_menu_item', $menu_item );
}
Better yet, use a filter to add the slug to a category menu point:
<?php
// fix for: https://github.com/unfulvio/wp-api-menus/issues/42
add_filter('rest_menus_format_menu_item', function($item){
if($item['type'] == 'taxonomy'){
$term = get_term($item['object_id'], $item['object']);
$item['object_slug'] = $term->slug;
}
return $item;
}, 10, 1);
Hi,
I have a similar issue with a menu item where the type is post_type_archive
. This is a page from a plugin.
It triggers a notice at the top of the response which makes it "invalid" for the consumer:
< br / > < b > Notice < /b>: Trying to get property of non-object in <b>/[...]/wp-content/plugins/wp-api-menus/includes/wp-api-menus-v2.php < /b> on line <b>386</b > < br / > {
"ID": 2,
"name": "Main",
"slug": "main",
[...]
I think this is related to this issue because the code assume that get_post( $item['object_id'] )
will return something whereas get_post can an will return null if the post is not found for some reason.
I've made a hotfix branch on my fork here but I believe that it can be improved before making a pull request here.
What are all the type that can be post_type
, custom
, taxonomy
? Let's see if we can handle them all ⚗️ !
PS: The given filter does not work because it's done after the item has been formatted and the notice is triggered/added no matter what 😞 .
category term has "object_slug" null on endpoint page has "object_slug" defined properly