Reputation: 107
I have a piece of custom code for my magento solution that displays images and text in a static block element, allowing me to have a visual representation of a category view.
However even though the code should sort my categories according to their position in the admin panel this does not happen.
im sitting on a magento 1.9.x.x solution and this is my code hoping someone has some insight here.
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'image'))
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('include_in_menu', 1)//if not to show in nav
->addIdFilter($category->getChildren())
->addAttributeToSort(‘position’,'ASC');
?>
<?php $_columnCount = 4; ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="catblocks">
<?php foreach ($categories as $category): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first <?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $category->getUrl() ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>"
alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<span><?php echo $category->getName() ?></span>
</a>
<?php $i++ ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
Upvotes: 0
Views: 228
Reputation: 1122
In your code addAttributeToSort(‘position’,'ASC')
is using non-ASCII quotes around 'position' and the ending </ul>
should come before endif;
since the opening tag comes after the start of the if statement.
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'image'))
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('include_in_menu', 1)//if not to show in nav
->addIdFilter($category->getChildren())
->addAttributeToSort('position','ASC');
?>
<?php $_columnCount = 4; ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="catblocks">
<?php foreach ($categories as $category): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first <?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $category->getUrl() ?>">
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>"
alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<span><?php echo $category->getName() ?></span>
</a>
<?php $i++ ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Upvotes: 1