1

I'm trying to add the class "first" and "last" to my categories on the frontpage, but it only adds the "first" class - AND to all of the list items. Can someone please help me with this?

My .phtml looks like this:

<?php $_categories=$this->getStoreCategories();?>
<div class="category-grid">
    <h3><span class="leftline"></span><span class="middle">Her kan du købe</span><span class="rightline"></span></h3>
    <ul>
        <?php $_columnCount = $this->getColumnCount(); ?>
        <?php $i=0; foreach ($_categories as $_category): ?>
        <?php if($_category->getIsActive()): ?>
        <?php $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); ?>
        <?php $layer = Mage::getSingleton('catalog/layer'); ?>
        <?php $layer->setCurrentCategory($cur_category); ?>
        <?php if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
        <?php if ($i++%$_columnCount==0): ?>
        <li class="list-item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
            <a href="<?php echo $this->getCategoryUrl($_category)?>" title="<?php echo $this->htmlEscape($_category->getName()); ?>">
                <span class="linkimage"><img src="<?php echo $_imageUrl ?>" alt="<?php echo $_category->getName()?>" /></span>
                <span class="linktitle"><?php echo $_category->getName()?></span>
            </a>
        </li>
        <?php endif; ?>
        <?php endif; ?>
        <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</div>
1

<?php if ($i++%$_columnCount==0): ?>
Doesn't this mean that the <li> element will be added only if $i%_columnCount is 0. Then it will look through the categories and print the next one that matches this condition? In this case you will never have the 'last' column.
Try this:

<?php $_categories=$this->getStoreCategories();?>
<div class="category-grid">
    <h3><span class="leftline"></span><span class="middle">Her kan du købe</span><span class="rightline"></span></h3>
    <ul>
        <?php $_columnCount = $this->getColumnCount(); ?>
        <?php $i=0; foreach ($_categories as $_category): ?>
        <?php if($_category->getIsActive()): ?>
        <?php $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); ?>
        <?php $layer = Mage::getSingleton('catalog/layer'); ?>
        <?php $layer->setCurrentCategory($cur_category); ?>
        <?php if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
        <li class="list-item<?php if($i%$_columnCount==0): ?> first<?php elseif(($i+1)%$_columnCount==0): ?> last<?php endif; ?>">
            <a href="<?php echo $this->getCategoryUrl($_category)?>" title="<?php echo $this->htmlEscape($_category->getName()); ?>">
                <span class="linkimage"><img src="<?php echo $_imageUrl ?>" alt="<?php echo $_category->getName()?>" /></span>
                <span class="linktitle"><?php echo $_category->getName()?></span>
            </a>
            <?php $i++;?>
        </li>
        <?php endif; ?>
        <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</div>
  • You might be on to something, but this wasn't the solution. The "first" class is still the only class being added, and still to all list items. – Thomas Urup Schjerlund Dec 9 '13 at 9:19
  • @ThomasUrupMichaelsen. probably. I didn't test the code, but I think the idea is good. I will investigate. Just to get it. You are listing only categories with images? – Marius Dec 9 '13 at 9:22
  • I'm only listing categories with images and title, yes. Thanks mate – Thomas Urup Schjerlund Dec 9 '13 at 9:24
  • @ThomasUrupMichaelsen. Make sure that column count is not 1. If it is it won't work because you will always get $i%$_columnCount==0 and you will never reach the elseif statement – Marius Dec 9 '13 at 9:24
  • Thanks mate, I figured out, that I need to add the columncount to the CMS block, I've added! Thanks a lot for pointing me in the right direction. – Thomas Urup Schjerlund Dec 9 '13 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.