In Magento 2 there are several ways to get a WordPress menu, build it and display it.
Get a WordPress Menu as an Array
<?php
// You can use constructor injection but this tutorial
// uses the object manager for simplicity
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Get a Menu model object
$menu = $objectManager->create('FishPig\WordPress\Model\MenuFactory')->create();
// Load the menu by it's ID
// You can get the Menu ID from the URL while editing
// the menu in the WordPress Admin
$menu->load(3);
// Check that the menu has been loaded
if ($menu->getId()) {
// Load the menu items as an array tree
$menuTree = $menu->getMenuTreeArray();
// This will print out the menu items
print_r($menuTree);
// You can also get the array as objects rather than static text
$menuObjects = $menu->getMenuTreeObjects();
}
else {
echo 'No menu exits in WordPress with the given ID.';
}
?>
Get a WordPress Menu as HTML
This method will provide you with the menu in HTML format. The HTML format is fixed and may not match the HTML required for your template so the above method is recommended over this.
<?php
// You can use constructor injection but this tutorial
// uses the object manager for simplicity
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Get the menu block
$menuBlock = $objectManager->create('FishPig\WordPress\Block\Sidebar\Widget\NavMenuFactory')->create();
// Set the menu ID
$menuBlock->setNavMenu(3);
// Print out the menu HTML
echo $menuBlock->toHtml();
?>