thebatclaudio / wp-rest-api-v2-menus

Adding menus endpoints on WP REST API v2
47 stars 22 forks source link

post_name of item inconsistency #4

Closed mgiraldo closed 6 years ago

mgiraldo commented 6 years ago

i am using this plugin with wp page types. menus are created properly but at some point the post_name for each items went from being the slug to the id. eg: from post_name: "about-us" to post_name: "6893". the logic in the app requires it to be the slug. old pages still show the slug but any new pages i create appear in the menu with their id. any idea what could have gone wrong?

mgiraldo commented 6 years ago

ok so turns out the reason is that if the menu item is a page, the post_name is the page ID whereas if I make a “Custom Link” and point it to the corresponding /wp-json/wp/v2/pages/ID link then the post_name is the slug.

thebatclaudio commented 6 years ago

Hi, I'm using the wp_get_nav_menu_items Wordpress function. As you said, if the page hasn't got a slug it will return its id instead of its slug.

mgiraldo commented 6 years ago

but the pages do have slugs:

image

or is there another field somewhere that i'm not adding?

thebatclaudio commented 6 years ago

As you can see in wp-rest-api-v2-menus.php on line 36, I simply populate the items attribute using wp_get_nav_menu_items function, that is a standard Wordpress function. I don't know the reason, but it work in this way. You can use /wp-json/wp/v2/pages/:object_id endpoint to get page's data and get your slug.

FDiskas commented 5 years ago

So sad. Can't generate menus with correct urls in SPA. The best what I can get is the url. And that url of course is my rest api endpoint :( :ambulance: