简体   繁体   English

Magento 2 WordPress集成(FishPig扩展名)-Magento中的添加菜单

[英]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. 我使用了magento 2的fishPig Magento WordPress集成。在第一个版本中,建立了在magento中添加菜单的功能。 In the second version its incomplete and the dev pointed me to the model Menu.php in order to get this achieved. 在第二版中,它不完整,开发人员将我指向Menu.php模型,以实现此目的。 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/ 扩展名: https : //fishpig.co.uk/magento-2/wordpress-integration/

Take a look at Model/Menu.php. 看一下Model / Menu.php。 From there on i have to build it up in order to get the WP menus out in the frontend. 从那以后,我必须进行构建,以便在前端获得WP菜单。 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.php块,我认为首先我必须建立该块,然后是xml和phtml模板

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. 然后,您可以使用所需的HTML结构递归遍历数组以构建菜单。 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. 如果要将菜单添加到现有菜单中,这将很有用,因为它可以让您匹配现有菜单使用的HTML结构。

// 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: 如果您不关心使用什么HTML结构,则可以使用以下代码块:

\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. 如果浏览此块的代码,将看到它检查数据项“ nav_menu”的菜单ID。 You can set this via XML (if you created the block via XML) or create the whole thing using PHP: 您可以通过XML进行设置(如果您是通过XML创建的块),也可以使用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. 尽管此解决方案迫使您使用特定的HTML结构,但它不如原始解决方案好。 You would be much better using the first solution, as this will allow you to use any HTML structure that you require. 使用第一种解决方案会更好,因为这将允许您使用所需的任何HTML结构。 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. 您的主题可能已经具有包含CSS规则和JS的菜单,因此您可以使用此方法以正确的HTML结构构建菜单。

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

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