Pergunta

I have 4 main categories in an opencart site.

I want to have a different header image for the 4 separate categories. How can I get the current category name and put an if statement to select the image for the header?

Current header image code:

<div id="headerWrapper">
<div id="header">
    <div class="div1">
        <div class="div2">
        <?php if ($logo) { ?>
        <a href="<?php echo str_replace('&', '&amp;', $home); ?>"><img src="<?php echo $logo; ?>" title="<?php echo $store; ?>" alt="<?php echo $store; ?>" /></a>
        <?php } ?>
        </div>        
    </div>
</div>

Foi útil?

Solução

You will need to work out if the path is set and the route is product/category to begin with, to check if you are even on a category page. Then you will need to use that information in a switch really (rather than a large if else listing). Can you give more detail on what it is you are wanting to change in the code above? Is it the logo or are you wanting to add a class to the header that you can then use to style via your stylesheet

To find out if you are in the category, use this in the index() method in catalog/controller/common/header.php

$get = $this->request->get;
$this->data['cat_id'] = false;
$this->data['cat_name'] = '';
if(!empty($get['route']) && $get['route'] == 'product/category' && !empty($get['path'])) {

    $cats = explode('_', $get['path']);
    $cat_id = array_pop($cats);
    $cat_id = (int) $cat_id;

    if($cat_id) {
        $this->load->model('catalog/category');
        $result = $this->model_catalog_category->getCategory($cat_id);
        $this->data['cat_id'] = $cat_id;
        $this->data['cat_name'] = $result['name'];
    }
}

This will give you two variables to use in your template's common/header.tpl file

$cat_id and $cat_name

$cat_id will be the current category id, or false if it's not a category page

$cat_name will have the current category name if one exists, or an empty string if it doesn't

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top