简体   繁体   English

使用JavaScript在Wordpress网站中获取菜单项

[英]Getting menu items in a wordpress site with javascript

I'm trying to see if I can query the wordpress API to get all of the items in the menu of the site using Javascript. 我正在尝试查看是否可以查询wordpress API以使用Javascript获取网站菜单中的所有项目。

I can see that there is a post here on how to do this in php but I don't know how to translate this to javascript as it seems to be using the internal wordpress PHP API. 我可以看到这里有一篇关于如何在php中执行此操作的文章,但是由于它似乎正在使用内部wordpress PHP API,因此我不知道如何将其转换为javascript。

There is a great library here and I can see that with the following code I can query the site: 这里有一个很棒的库 ,我可以看到以下代码可以查询该站点:

var site = Site('<id>', WPCOM);
site.get(function(err, info){
  // `info` data object
});

But I don't know how to query the menu items or if this is at all possible? 但是我不知道如何查询菜单项,或者根本不可能吗?

Any help would be appreciated. 任何帮助,将不胜感激。

I don't have the rep to put this link in a comment. 我没有代表将该链接放在评论中。 I have not used it and don't know if it will do what you need but how about this page: https://wordpress.org/plugins/wp-api-menus/ 我还没有使用过它,也不知道它是否可以满足您的需求,但是如何处理此页面: https : //wordpress.org/plugins/wp-api-menus/

Since I am posting as an answer, here is some info from that page: 由于我是作为回答发布的,因此以下是该页面上的一些信息:

This plugin extends the WordPress JSON REST API with new routes for WordPress registered menus.

The new routes available will be:

       /menus list of every registered menu.
       /menus/<id> data for a specific menu.
       /menu-locations list of all registered theme locations.
       /menu-locations/<location> data for menu in specified menu in theme location.

I also found this similar post on wordpress.stackexchange: https://wordpress.stackexchange.com/questions/209381/get-wp-navigation-menu-from-rest-api-v2 . 我也在wordpress.stackexchange上找到了类似的帖子: https : //wordpress.stackexchange.com/questions/209381/get-wp-navigation-menu-from-rest-api-v2 It ends up pointing to the same 'wp-api-menus/' link. 最终指向相同的“ wp-api-menus /”链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM