【问题标题】:Making category image as thumb in menu, OpenCart 2.0.x在菜单中制作类别图像作为拇指,OpenCart 2.0.x
【发布时间】:2016-02-25 07:35:42
【问题描述】:

我很难做到这一点。我要做的是我希望菜单中的类别图像作为 Opencart 2.0.x 中的拇指。

我已将 'thumb' => $thumb, 放入 catalog/controller/common/header.tpl

像这样:

        // Menu
    $this->load->model('catalog/category');

    $this->load->model('catalog/product');

    $data['categories'] = array();

    $categories = $this->model_catalog_category->getCategories(0);

    foreach ($categories as $category) {
        if ($category['top']) {
            // Level 2
            $children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
                $filter_data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $children_data[] = array(
                    'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                    'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                );
            }

            // Level 1

        $this->load->model('tool/image');
        $image = empty($category['image']) ? 'no_image.jpg' : $category['image'];
        $thumb = $this->model_tool_image->resize($image, 100, 100);

            $data['categories'][] = array(
                'name'     => $category['name'],
                'children' => $children_data,
                'thumb'    => $thumb,
                'column'   => $category['column'] ? $category['column'] : 1,
                'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
            );
        }
    }

我还把 $category['thumb'] 放在 catalog/view/theme/default/template/common/header.tpl

像这样:

                <?php if ($categories) { ?>
            <nav id="navigation">
                <div class="boxed bg-navigation">
                    <div class="container">
                        <ul data-breakpoint="800" class="flexnav with-js lg-screen clearfix">
                            <?php foreach ($categories as $category) { ?>
                            <?php if ($category['children']) { ?>
                            <li><a href="<?php echo $category['href']; ?>" ><?php echo $category['name']; ?></a>
                                <ul class="list-unstyled ul-col-<?php echo $category['column']; ?>">
                                    <?php foreach (array_chunk($category['children'], ceil(count($category['children']) / $category['column'])) as $children) { ?>
                                    <?php if ($category['column'] == 1) { ?> 
                                    <?php foreach ($children as $child) { ?>
                                    <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li>
                                    <?php } ?>
                                    <?php } else { ?>
                                    <li class="li-inline">
                                        <div>
                                            <?php foreach ($children as $child) { ?>
                                            <?php $category['thumb']?>
                                            <a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a>
                                            <?php } ?>
                                        </div>
                                    </li>
                                    <?php } ?>
                                    <?php } ?>
                                </ul>
                            </li>
                            <?php } else { ?>
                            <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a></li>
                            <?php } ?>
                            <?php } ?>
                        </ul>

                    </div>
                </div>
            </nav>
            <?php } ?>

我在这里做错了什么?我在 Stack Overflow Solution

上找到了这个解决方案

但我似乎无法正确解决此问题,这可能是一件我看不到的简单事情。我之前使用 OpenCart 1.5.6.4 中的另一个框架完成了此操作。但这也没有帮助我很多呵呵。如果你愿意,我可以将其他代码粘贴到。

【问题讨论】:

    标签: image opencart2.x


    【解决方案1】:

    我遇到了同样的问题,并在 Opencart 2.1.0.1 上运行 这是目录/controller/common/header.php 文件:

    (line 113)
    // Level 2
                $children_data = array();
    
                $children = $this->model_catalog_category->getCategories($category['category_id']);
    
                foreach ($children as $child) {
                    $filter_data = array(
                        'filter_category_id'  => $child['category_id'],
                        'filter_sub_category' => true
                    );
    
                    $this->load->model('tool/image');
                    $image = empty($child['image']) ? 'no_image.png' : $child['image'];
                    $thumb = $this->model_tool_image->resize($image, 100, 100);
    
                    $children_data[] = array(
                        'name'  => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                        'thumb'    => $thumb,
                        'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
                    );
                }
    

    这是我的目录/视图/主题/default/template/common/header.tpl 文件:

    (line 108)
    <li><a href="<?php echo $child['href']; ?>"><img src="<?php echo $child['thumb']?>" alt="<?php echo $child['name']; ?>"/><br /><?php echo $child['name']; ?></a></li>
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这是因为$category['thumb']在模板文件中只包含图像源

      替换&lt;?php $category['thumb']?&gt;

      &lt;img src="&lt;?php echo $category['thumb']?&gt;" alt="&lt;?php echo $child['name']; ?&gt;"/&gt;

      此外,在控制器中,您正在设置父类别的拇指,并在子类别的模板打印中设置,因此它将一次又一次地打印相同的父图像。

      no_image.jpg 在最新版本中被替换为no_image.png

      【讨论】:

      • 我尝试了你的答案,但我得到了通知:未定义的索引:如果我更改为我们所写的内容,则为图像或拇指。我已经尝试了一切。并更改了一些东西,但似乎没有任何工作正常。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多