halsfeger VIP
Total posts: 3
10 Nov 2014 13:07

Hello

I want to change the Class of a div when specific category is used.

So i can display the category like this:

                <?php if($params->get('tmpl_core.item_categories') == 1 && $this->section->categories):?>
                <div class="cob-cat-th"><?php echo implode(', ', $item->categories);?></div>
            <?php endif;?>

But now i want to change the div class if the Category Changes, for example, if the Category is "News" then i want to have a div class "cat-news"

How can i mange this?

Regards

Halsfeger

Last Modified: 11 Nov 2014


pepperstreet VIP
Total posts: 3,837
10 Nov 2014 22:52

Welcome to the world of Cobalt ;)

May I ask where you need this output? Which template and class do you want to change? Your code snippet seems to be from the default list template. And it seems to be the table header... doesn't make sense to me. Can you give some more details, please?


Sergey
Total posts: 13,748
11 Nov 2014 04:09

$item->categories is an array. So you can do something like this.

<?php if($params->get('tmpl_core.item_categories') == 1 && $this->section->categories):?>
    <div class="cob-cat-th <?php echo mySpecialClass($item->categories); ?>"><?php echo implode(', ', $item->categories);?></div>
<?php endif;?>

And then at the end of your template

function mySpecialClass($cats) {
    if(!empty($cats[10])) {
        return 'cat-news';
    }
    if(!empty($cats[11])) {
        return 'cat-blog';
    }
}

where 10 and 11 are IDs of the categories. Anothre way is to add classes with IDs

function mySpecialClass($cats) {
    $keys = array_keys($cats);

    return 'cat-'.implode(' cat-', $keys);
}

You will have classes like cat-10 or if article in multy categories cat-10 cat-11.

Powered by Cobalt