问题
Recently I installed Opencart 1.5.6 for the first time. Everything in ok except that I trying to show images along with text in sub-categories ( where is Refine Search text ).
So far I've put this in catalog\controller\module\category.php
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'image' => $category['image'],
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
$this->data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
'image' => $category['image'],
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
New thing is 'image' => $category['image'],
. I also change a little bit catalog\view\theme\MYTHEME\template\product\category.tpl
like this:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<img src="../../../../../../image/data/models/<?php echo $category['image']; ?>" width="100"/>
<a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
</li>
<?php } ?>
</ul>
<ul>
<?php $j = $i + ceil(count($categories) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($categories[$i])) { ?>
<li>
<img src="../../../../../../image/data/models/<?php echo $categories['image']; ?>" width="100"/>
<a href="<?php echo $categories[$i]['href']; ?>"><?php echo $categories[$i]['name']; ?></a></li>
<?php } ?>
<?php } ?>
</ul>
The result is box with broken image and text under the image.

I can see from error what is it but can't figure out how to repair it
<img src="../../../../../../image/data/models/<b>Notice</b>: Undefined index: image in <b>..\catalog\view\theme\mytheme\template\product\category.tpl</b> on line <b>29</b>" width="100"/>
回答1:
Try something like in default OpenCart code.
Step 1
Open file catalog/view/theme/<your theme>/template/template/category.tpl
Find : Refine Category code.
Add after <div class="category-list">
start div
<?php $counter = 0; foreach ($categories as $category) {?>
<div>
<?php if ($category['thumb']) { ?>
<a href="<?php echo $category['href']; ?>"><img src="<?php echo $category['thumb']; ?>" alt="<?php echo $category['name']; ?>" /></a>
<?php } else { ?>
<a href="<?php echo $category['href']; ?>"><img src="image/no_image.jpg" alt="<?php echo $category['name']; ?>" /></a>
<?php } ?>
<a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
</div>
<?php $counter++; } ?>
Step 2
Open file catalog/controller/product/category.php
Find :
$product_total = $this->model_catalog_product->getTotalProducts($data);
Add after
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'));
Step 3
In the same file catalog/controller/product/category.php
Find :
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
Replace (instead of above line)
'href' => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url), 'thumb' => $image
and then check it.
来源:https://stackoverflow.com/questions/24652988/showing-images-in-subcategories-in-opencart