【问题标题】:Magento - Put an image next to top category menu nameMagento - 在顶级类别菜单名称旁边放一张图片
【发布时间】:2012-04-12 17:17:30
【问题描述】:

我想在我的每个类别和子类别名称旁边放一个小图像图标(不要用图像替换类别名称,只需在类别名称后面放一个图像)。我已经尝试过覆盖 app/core/code/Mage/Catalog/Block/Navigation.php

我更改了_renderCategoryMenuItemHtml,之后在styles.css中引用category_id,我准备在代码所在的位置添加背景图片

 $htmlLi .= '>';
$html[] = $htmlLi;

$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';

$htmlLi .= '>';
$html[] = $htmlLi;

$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span class="category_'.$this->getCurrentCategory()->getId().'">'.$this->escapeHtml($category->getName()).'</span>';
$html[] = '</a>';

但是努力工作。我相信很多人可能想在类别名称之后显示一个小缩略图。但只是不知道任何简单或直接的方法。

感谢任何帮助。谢谢。

【问题讨论】:

    标签: php html css magento


    【解决方案1】:

    为什么不使用 CSS 背景图像?

    加载模板/目录/导航/top.phtml并为每个级别的

  • 项添加一个新的类或id,可能类似于:
    class="nav-<?php echo $_category->getName() ?>"
    

    然后将这些类添加到您的 CSS 中,即:

    .nav-shoes { background: url(images/nav-shoes.png) no-repeat; }
    

    或者更好的是,使用您要使用的所有图像构建一个精灵图像:

    [class^="nav-"] { background: url(images/sprite.png) no-repeat; }
    .nav-shoes { background-position: 10px 10px; }
    

    那么你需要为每张图片找出背景位置。

  • 【讨论】:

      【解决方案2】:

      将此解决方案用于 Magento -1.8.*

      转到 intp 模型文件。 (/app/code/core/Mage/Catalog/Model/Observer.php)

      在函数名称中:_addCategoriesToMenu

      更新以下代码

        $categoryData = array( 
              'name' => $category->getName(),
              'id' => $nodeId,
              'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
              'is_active' => $this->_isActiveMenuCategory($category),
              'thumbnail' => Mage::getModel('catalog/category')->load($category->getId())->getThumbnail()
      
          );
      

      然后进入 Html 文件夹。 (app/code/core/Mage/Page/Block/Html/Topmenu.php)

      在函数名称的第 128 行:_getHtml

      更新以下代码行

          if($childLevel < 1 ){
              $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
              $img = '<img src="'.$urls.'" />';
          }
      
          $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
          $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
              . $this->escapeHtml($child->getName()) . ' </span> '.$img.' </a>';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        相关资源
        最近更新 更多