顶级菜单上的Prestashop子类别图像

时间:2017-02-16 14:55:04

标签: php prestashop prestashop-1.6

在Prestashop中,我们可以将图像(拇指)添加到子类别菜单

if ((int)$category['level_depth'] > 1 && !$is_children) {
                $files = scandir(_PS_CAT_IMG_DIR_);

                if (count(preg_grep('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $files)) > 0) {
                    $html .= '<li class="category-thumbnail">';

                    foreach ($files as $file) {
                        if (preg_match('/^'.$category['id_category'].'-([0-9])?_thumb.jpg/i', $file) === 1) {
                            $html .= '<div><img src="'.$this->context->link->getMediaLink(_THEME_CAT_DIR_.$file)
                            .'" alt="'.Tools::SafeOutput($category['name']).'" title="'
                            .Tools::SafeOutput($category['name']).'" class="imgm" /></div>';
                        }
                    }

                    $html .= '</li>';
                }
            }

不幸的是,这段代码对我不起作用,图像没有显示,虽然我为子类别设置了拇指图像。 有帮助吗?

2 个答案:

答案 0 :(得分:0)

这是我的解决方案,它与我合作

protected function generateCategoriesMenu($categories, $is_children = 0)
{
    $html = '';

    foreach ($categories as $key => $category) {
        if ($category['level_depth'] > 1) {
            $cat = new Category($category['id_category']);
            $link = Tools::HtmlEntitiesUTF8($cat->getLink());
        } else {
            $link = $this->context->link->getPageLink('index');
        }

        /* Whenever a category is not active we shouldnt display it to customer */
        if ((bool)$category['active'] === false) {
            continue;
        }

        $html .= '<li'.(($this->page_name == 'category'
            && (int)Tools::getValue('id_category') == (int)$category['id_category']) ? ' class="sfHoverForce"' : '').'>';
        $html .= '<a href="'.$link.'" title="'.$category['name'].'">';
        if($category['level_depth'] == '3' AND Tools::file_exists_cache(_PS_CAT_IMG_DIR_.(int)$category['id_category'].'_thumb.jpg'))
            $html .= '<img src="/img/c/'.(int)$category['id_category'].'_thumb.jpg'.'" class="imgm" height="30" /><br>';
        $html .= $category['name'];

        $html .='</a>';
        if (isset($category['children']) && !empty($category['children'])) {
            $html .= '<ul>';
            $html .= $this->generateCategoriesMenu($category['children'], 1);
            $html.= '<li class="sfHoverForce">'.$category['promo_right'].'</li>';
            $html .= '</ul>';
        }


        $html .= '</li>';
    }

    return $html;
}

答案 1 :(得分:0)

我想通过编辑ps_mainmenu模板文件与您分享我在Prestashop 1.7上的解决方案:

<div {if $depth === 0} class="popover sub-menu js-sub-menu collapse"{else} class="collapse"{/if} id="top_sub_menu_{$_expand_id}">
  {menu nodes=$node.children depth=$node.depth parent=$node}
       {if $node.image_urls}
                {foreach from=$node.children depth item=mychild}
                      {foreach from=$mychild.image_urls item=image_url}
                            <img src="{$image_url}" title="" alt="" />
                      {/foreach}
                {/foreach}
       {/if}

在这里查看我的文章:

https://www.prestasoo.com/blog/how-to-show-subcategories-images-in-prestashop-s-top-menu.html