How to display Category images in OpenCart

久未见 提交于 2020-01-23 21:22:55

问题


My categories.php module in controller:

        $this->data['categories'][] = array(
            'category_id'       => $category['category_id'],
            'name'              => $category['name'],
            'image'             => $category['image'],
            'children'          => $children_data,              
            'href'              => $this->url->link('product/category', 'path=' . $category['category_id'])
        );  

My categories.tpl in /products/ (View)

  <?php foreach ($categories as $category) { ?>
  <li><a href="<?php echo $category['href']; ?>"><img src="<?php echo $category['image']; ?>" alt="<?php echo $category['name']; ?>"></a></li>
  <?php } ?>

However it just gives me an error saying that the variable is unknown.

Am I doing something wrong?


回答1:


  1. The First problem is that you are editing the wrong controller file, you should edit the controller/product/category.php if you want your changes to appear in that specific view.

  2. The images will not appear as it is since it is not saved as a full url in the database:

    insted of => $category['image']

    it should be => DIR_IMAGE.$category['image']

Hope this solves your answer.




回答2:


Your approach was good but missing something - it is good to resize the image between displaying so that users need to download less (or smaller) resources when browsing Your store.

Instead of

            'image'             => $category['image'],

use this

            'image'             => $this->model_tool_image->resize($category['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height')),

This will ensure the images are resized to the dimensions defined in administration and will append DIR_IMAGE in front of the image path.

If this line is missing in Your source code:

$this->load->model('tool/image');

make sure You add it before the category image is going to be resized.



来源:https://stackoverflow.com/questions/23770582/how-to-display-category-images-in-opencart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!