简体   繁体   中英

Magento Category sorting

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.xx 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>

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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM