简体   繁体   中英

Magento 2 WordPress Integration (FishPig Extension) - Add menu in Magento

i use the fishPig Magento WordPress Integration for magento 2. In the first version the functionality to add menus in magento was build up. In the second version its incomplete and the dev pointed me to the model Menu.php in order to get this achieved. Need some help guys, in order to get pointed into the right direction. Somebody the same issue?

Extension: https://fishpig.co.uk/magento-2/wordpress-integration/

Take a look at Model/Menu.php. From there on i have to build it up in order to get the WP menus out in the frontend. Unfortunately there is no Menu.php Block, i think first i have to build up the block and then the xml and phtml templates

Menu's are not incomplete, you just haven't checked the code properly.

The following code will load an array of the menu. This array contains all of the information you need to build the menu. You can then recursively loop through the array to build the menu using the HTML structure that you require. This is useful if you are adding this menu to an existing menu as it allows you to match the HTML structure used by your existing menu.

// This uses the object manager
// A better way would be to inject the MenuFactory in your constructor
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$menu = $objectManager->create('FishPig\WordPress\Model\MenuFactory')->create();

$menuId = 3;

if ($menu->load($menuId)->getId()) {
    echo sprintf('<pre>%s</pre>', print_r($menu->getMenuTreeArray(), true));
}
else {
    echo 'No menu exists in WordPress with an ID of ' . (int)$menuId;
}

If you don't care what HTML structure is used, you can use the following block:

\FishPig\WordPress\Block\Sidebar\Widget\NavMenu

If you look through the code of this block, you will see it checks the data item 'nav_menu' for the menu ID. You can set this via XML (if you created the block via XML) or create the whole thing using PHP:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$menuBlock = $objectManager->create('FishPig\WordPress\Block\Sidebar\Widget\NavMenuFactory')->create();

$menuId = 3;

echo $menuBlock->setNavMenu($menuId)->toHtml();

This solution isn't as good as the original solution though as it forces you to use a specific HTML structure. You would be much better using the first solution, as this will allow you to use any HTML structure that you require. Your theme probably already has a menu with CSS rules and JS in place so you could use this method to build the menu in the correct HTML structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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