php 将静态链接添加到 Magento 中的顶部导航菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10111389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:23:56  来源:igfitidea点击:

Add Static Links to Top Navigation Menu in Magento

phpmagento

提问by michaelmcgurk

In my Magento install, I have a dropdown menu that is generated from a Products category in my ‘Categories'. It uses the code: <?php echo $this->getChildHtml('topMenu') ?>to display the menu.

在我的 Magento 安装中,我有一个从“类别”中的产品类别生成的下拉菜单。它使用代码: <?php echo $this->getChildHtml('topMenu') ?>来显示菜单。

However, at certain intervals, I'd like to add a static HTML link (list item with hyperlink) in to the menu.

但是,每隔一段时间,我想在菜单中添加一个静态 HTML 链接(带有超链接的列表项)。

How do I achieve this?

我如何实现这一目标?

Many thanks for any pointers

非常感谢您的指点

回答by Greg Demetrick

In the current version of Magento you can do this by creating a new category then redirecting that category to any URL you want (CMS page, Product page, offsite, etc.)

在当前版本的 Magento 中,您可以通过创建一个新类别然后将该类别重定向到您想要的任何 URL(CMS 页面、产品页面、非现场等)来实现这一点。

  1. Create a new Subcategory. Name this how you want it to show up in the menu. Make sure it has a unique URL key, it is active, and it is "In Navigation Menu". You can change where it shows up by changing it's location in the category menu by drag-n-drop to the place you want it to show.
  2. Go into URL Rewrite Management. Your new category should be the top system link that was just created. If not, filter the table by the id number of the new category. Go into this new redirection.
  3. Change the Redirect to Permanent and copy the Target Path. You will need this for the next step. Save your changes.
  4. Create a new custom URL Redirect with the following options and save it
    • Store: The store this applies to. Must match the store of the category you are redirecting
    • ID Path: URL you want the category to go to. (i.e. gift-cards)
    • Request Path: Paste the Target Path you copied from the previous step here
    • Target Path: URL you want the category to go to. (i.e. gift-cards)
    • Redirect: Set to Permanent 301
  5. Go to Index Management and refresh your Catalog URL Rewrites (if needed)
  1. 创建一个新的子类别。将此命名为您希望它在菜单中显示的方式。确保它有一个唯一的 URL 键,它处于活动状态,并且它是“在导航菜单中”。您可以通过将其拖放到您希望它显示的位置来更改它在类别菜单中的位置来更改它的显示位置。
  2. 进入 URL 重写管理。您的新类别应该是刚刚创建的顶级系统链接。如果不是,则按新类别的 id 号过滤表。进入这个新的重定向。
  3. 将重定向更改为永久并复制目标路径。您将在下一步中需要它。保存您的更改。
  4. 使用以下选项创建新的自定义 URL 重定向并保存
    • 商店:这适用于商店。必须与您要重定向的类别的商店相匹配
    • ID 路径:您希望类别转到的 URL。(即礼品卡)
    • 请求路径:在此处粘贴您从上一步复制的目标路径
    • 目标路径:您希望类别转到的 URL。(即礼品卡)
    • 重定向:设置为永久 301
  5. 转到索引管理并刷新您的目录 URL 重写(如果需要)

You should now be able to go to the Frontend and see the new category in the menu. Clicking on it will redirect you straight to the link you provided. I prefer this method over others because it gives you better control over where a static link gets placed and allows you to put a staticly linked menu item within sub-menus.

您现在应该能够转到前端并在菜单中看到新类别。单击它会将您直接重定向到您提供的链接。我更喜欢这种方法而不是其他方法,因为它可以让您更好地控制静态链接的放置位置,并允许您在子菜单中放置静态链接的菜单项。

This video shows this technique step-by-step for Magento Go but the process is the same for all flavors of Magento. http://www.youtube.com/watch?v=5rSIQlRC8Xk

该视频逐步展示了 Magento Go 的这项技术,但该过程对于所有版本的 Magento 都是相同的。 http://www.youtube.com/watch?v=5rSIQlRC8Xk

回答by Omar Faruk Sharif

You can try this method. I think this way is more proper than others.

你可以试试这个方法。我认为这种方式比其他方式更合适。

http://www.atwix.com/magento/how-to-add-a-new-item-to-the-navigation-menu/

http://www.atwix.com/magento/how-to-add-a-new-item-to-the-navigation-menu/

回答by evensis

There's a few ways to do this, you can rewrite Mage_Catalog_Block_Navigation to achieve this.

有几种方法可以做到这一点,您可以重写 Mage_Catalog_Block_Navigation 来实现这一点。

Inside the renderCategoriesMenuHtml function, you can catch it at the various positions within the foreach loop with a counter and render a block:

在 renderCategoriesMenuHtml 函数中,您可以使用计数器在 foreach 循环内的各个位置捕获它并渲染块:

if ($j == 1) {
    $html .= $this->getLayout()->createBlock('namespace/yourmodule')->setTemplate('topMenu/link.phtml')->toHtml();
}

Or literally render the link like so:

或者像这样直接渲染链接:

$html .= '<a href="yourlink.php">Link Name</a>';

The first method gives a bit more flexibility for frontend developers, rather than hardcoding it in the class.

第一种方法为前端开发人员提供了更多的灵活性,而不是在类中对其进行硬编码。

You can also implement some serious manipulation through this method such as adding images into the menu, I used something similar to implement a 'featured products' feature in the menu.

您还可以通过此方法实现一些严肃的操作,例如将图像添加到菜单中,我使用类似的东西在菜单中实现了“特色产品”功能。

回答by subash pandey

how to add magento top menu hyperlink title tagsopen file and look code

如何添加 magento 顶部菜单超链接标题标签打开文件并查看代码

$html .= 'getUrl() . '" ' . $outermostClassCode . ' >' . $this->escapeHtml($child->getName()) . '';

$html .= 'getUrl() . '" ' . $outermostClassCode . ' >' . $this->escapeHtml($child->getName()) . '';

app/code/core/Mage/Page/Block/Html/Topmenu.php

app/code/core/Mage/Page/Block/Html/Topmenu.php

$html .= 'getUrl() . '" ' . $outermostClassCode . ' title="' . $this->escapeHtml($child->getName()) . '">' . $this->escapeHtml($child->getName()) . '';

$html .= 'getUrl() . '" ' . $outermostClassCode . ' title="' . $this->escapeHtml($child->getName()) 。'">' . $this->escapeHtml($child->getName()) . '';

回答by Dani Garval

You can create a block inside top.menuwith your custom links. Add this in your theme's local.xmlfile:

您可以top.menu使用自定义链接在内部创建一个块。将其添加到您的主题local.xml文件中:

<reference name="header">
    <reference name="top.menu">
        <block type="page/template_links" name="top.menu.custom-links">
            <action method="addLink" translate="label title">
                <label>New Link</label>
                <url>my-url</url>
                <title>New Link</title>
                <prepare>true</prepare>
                <urlParams helper="core/url/getHomeUrl"/>
                <position>60</position>
            </action>
        </block>
    </reference>
</reference>

I think this is the easiest way.

我认为这是最简单的方法。