【问题标题】:Category navigation in navigation child导航子项中的类别导航
【发布时间】:2014-04-14 18:47:14
【问题描述】:

我在 Magento 中的导航有问题。子类别以一种有趣的方式显示,我也不想要它们。请看以下截图:

http://tinypic.com/r/2l97byp/8

在左上角,子项显示在菜单中。但是我不希望他们扩展到一个新的区块,而只是显示在他们的父类别下方,如图所示:

tinypic.com/r/16l0kk3/8

为此我有两组代码,但是我不知道要编辑什么才能获得所需的结果。我试图让它工作,但没有运气。

两个代码部分:

top.phtml:

<?php
/**
 * Top menu for store
 *
 * @see Olegnax_Navigation_Block_Navigation
 */
?>
<?php
/**
 * $this->renderCategoriesMenuHtml() supports optional arguments:
 * int Level number for list item class to start from
 * string Extra class of outermost list items
 * string If specified wraps children list in div with this class
 */
?>
<!-- navigation BOF -->
<?php $_menu = $this->renderCategoriesMenuHtml(0, 'level-top', 'sub-wrapper' ) ?>
<?php if($_menu): ?>
<nav class="olegnax">
    <ul id="nav">
        <?php if (Mage::getStoreConfig('celebritysettings/celebritysettings_header/navigation_home')): ?>
         <li class="level0 level-top">
            <a href="<?php echo $this->getBaseUrl(); ?>"><span><?php echo $this->__('Home'); ?></span></a>
         </li>
     <?php endif; ?>
        <?php
        echo $_menu;
        $custom_tab = Mage::getModel('cms/block')->load('celebrity_navigation_block');
        if($custom_tab->getIsActive()) {
            echo '
            <li class="level0 level-top parent custom-block">
                <a href="#" class="level-top">
                    <span>'.$custom_tab->getTitle().'</span>
                </a>
                <div class="sub-wrapper">'.$this->getLayout()->createBlock('cms/block')->setBlockId('celebrity_navigation_block')->toHtml().'</div>
            </li>';
        }
        ?>
    </ul>
</nav>
<?php endif ?>
<!-- navigation EOF -->

还有 Navigation.phtml:

    <?php
/**
 * @version   1.0 12.0.2012
 * @author    Olegnax http://www.olegnax.com <mail@olegnax.com>
 * @copyright Copyright (C) 2010 - 2012 Olegnax
 */

class Olegnax_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation
{

    /**
     * columns html
     *
     * @var array
     */
    protected $_columnHtml;

    /**
     * Render category to html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @param boolean Whether ot not this item is first, affects list item class
     * @param boolean Whether ot not this item is outermost, affects list item class
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @param boolean Whether ot not to add on* attributes to list item
     * @return string
     */
    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
        $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
    {
        if (!$category->getIsActive()) {
            return '';
        }
        $html = array();

        // get all children
        if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        }
        $hasChildren = ($children && $childrenCount);

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);

        // prepare list item html classes
        $classes = array();
        $classes[] = 'level' . $level;
        $classes[] = 'nav-' . $this->_getItemPosition($level);
        if ($this->isCategoryActive($category)) {
            $classes[] = 'active';
        }
        $linkClass = '';
        if ($isOutermost && $outermostItemClass) {
            $classes[] = $outermostItemClass;
            $linkClass = ' class="'.$outermostItemClass.'"';
        }
        if ($isFirst) {
            $classes[] = 'first';
        }
        if ($isLast) {
            $classes[] = 'last';
        }
        if ($hasActiveChildren) {
            $classes[] = 'parent';
        }

        // prepare list item attributes
        $attributes = array();
        if (count($classes) > 0) {
            $attributes['class'] = implode(' ', $classes);
        }
        if ($hasActiveChildren && !$noEventAttributes) {
             $attributes['onmouseover'] = 'toggleMenu(this,1)';
             $attributes['onmouseout'] = 'toggleMenu(this,0)';
        }

        // assemble list item with attributes
        $htmlLi = '<li';
        foreach ($attributes as $attrName => $attrValue) {
            $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
        }
        $htmlLi .= '>';
        $html[] = $htmlLi;

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

        if ( $level == 0 ) {
            //get category description
            $ca = Mage::getModel('catalog/category')->load($category->getId());
            $description = $ca->getDescription();
            if ( empty($description) || !Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_description') ) {
                $columns = 4;
            } else {
                $columns = 2;
            }
            $columnItemsNum = array_fill(0, $columns, floor($activeChildrenCount / $columns));
            if ( $activeChildrenCount % $columns > 0 ) {
                for ($i = 0; $i < ($activeChildrenCount % $columns); $i++ ) {
                    $columnItemsNum[$i]++;
                }
            }
            $this->_columnHtml = array();
        }

        // render children
        $htmlChildren = '';
        $j = 0; //child index
        $i = 0; //column index
        $itemsCount = $columnItemsNum[$i];
        foreach ($activeChildren as $child) {

            if ( $level == 0 ) {
                $isLast = (($j+1) == $itemsCount || $j == $activeChildrenCount - 1);
                if ( $isLast ) {
                    $i++;
                    $itemsCount += $columnItemsNum[$i];
                }
            } else {
                $isLast = ($j == $activeChildrenCount - 1);
            }

            $childHtml = $this->_renderCategoryMenuItemHtml(
                $child,
                ($level + 1),
                $isLast,
                ($j == 0),
                false,
                $outermostItemClass,
                $childrenWrapClass,
                $noEventAttributes
            );
            if ( $level == 0 ) {
                $this->_columnHtml[] = $childHtml;
            } else {
                $htmlChildren .= $childHtml;
            }
            $j++;
        }

        if ( $level == 0 && $this->_columnHtml ) {
            $i = 0;
            foreach ( $columnItemsNum as $columnNum ) {
                $chunk = array_slice($this->_columnHtml, $i, $columnNum);
                $i += $columnNum;
                $htmlChildren .= '<li '.(count($this->_columnHtml) == $i ? 'class="last"' : '').'><ol>';
                foreach ( $chunk as $item ) {
                    $htmlChildren .= $item;
                }
                $htmlChildren .= '</ol></li>';
            }
        }
        if ( !empty($description) && !empty($htmlChildren) && Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_description') ) {
            $htmlChildren .= '<li class="menu-category-description clearfix">'.$description;
            if ( Mage::getStoreConfig('celebritysettings/celebritysettings_navigation/show_learn_more') ) {
                $htmlChildren .= '<p><button class="button" onclick="window.location=\''.$this->getCategoryUrl($category).'\'"><span><span>'.$this->__('learn more').'</span></span></button></p>';
            }
            $htmlChildren .= '</li>';
        }

        if (!empty($htmlChildren)) {
            if ($childrenWrapClass) {
                $html[] = '<div class="' . $childrenWrapClass . '">';
            }
            $html[] = '<ul class="level' . $level . '">';
            $html[] = $htmlChildren;
            $html[] = '</ul>';
            if ($childrenWrapClass) {
                $html[] = '</div>';
            }
        }

        $html[] = '</li>';

        $html = implode("\n", $html);
        return $html;
    } }

我希望有人可以看到要做什么才能对图像产生效果:)

最好的问候, 帕特里克

【问题讨论】:

  • 您在这里基本上问了两个不同的问题:Magento 类别显示代码是如何工作的,以及如何在随机网站的 CSS/HTML 代码中实现特定的布局效果。这两个问题本身都非常广泛。在一起,我怀疑你会得到你正在寻找的答案。
  • 这是一个非常具体的问题。据我所知,这与我的网站 CSS 无关,因为这都是在 top.phtml 中完成的。这是一个关于如何使用我的问题中的两个代码文件来达到图片所示效果的问题。我尝试了许多不同的更改,但没有运气。该问题与 stackoverflow.com/questions/21395096/… 密切相关,但也没有找到任何帮助。

标签: php magento navigation categories


【解决方案1】:

好吧,Alan Storm 是正确的,但就其价值而言,要回答您的问题“我要编辑哪个文件”,您编辑的文件是 Navigation.php。我这样说是因为 top.phtml 有echo $_menu; 这一行。无论如何,protected function _renderCategoryMenuItemHtml() 中的代码可能有点复杂,当我阅读代码并思考你想要实现的目标时,我认为你应该:

a) 尝试注释掉这些行:

if ($hasActiveChildren && !$noEventAttributes) {
     $attributes['onmouseover'] = 'toggleMenu(this,1)';
     $attributes['onmouseout'] = 'toggleMenu(this,0)';
}

为了移除鼠标悬停交互,然后看看你是否可以破解 CSS 以获得你想要的布局

b) 扔掉protected function _renderCategoryMenuItemHtml() 中的所有代码并重新开始:使用现有代码的元素重写它以显示您需要的 HTML 结构。

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 2017-10-14
    • 2020-10-08
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多