lets take a look at some code real quick and see if we can collectively diagnose the error residing within this code.

    $_category = array_unique($_category);
    foreach ($_category as $cat) {
            $category = $collection->load($cat);
            if($category->getName()) {
                echo '<a href="' . $category->getUrl() . '">More <strong>Of the ' . $category->getName() . '</strong></a>';
                echo 'test'

The above code is used to display category URLs along within their respective names to the user. Easy enough, right? However -- many of our products are shared between sites and as a result some category getNames do not resolve in one of our much smaller stores. This isn't an issue anywhere else, as far as I can tell, but here. When a the if($category->getName) boolean is found to be true for a category, but the next one is found to be false it will repeatedly output the previously echo'd statement.

So it will look something like this:


$_category={123,124,125,126} // only 124 is resolving to a name.



More of the same


More of the same


More of the same


After further testing it seems that the $category variable isn't changing after each iteration of the for look. I've even tried to unset($category) after every iteration with no success.

  • Do not load anything inside a loop if possible, it will make your site slower since the load is a resource consuming operation. Try gathering your collection before you loop through the object instead.
    – mbalparda
    Jan 6, 2015 at 19:59

2 Answers 2


Hm, the code looks weird... You are trying to load a category by ID from some collection. It's pretty weird... So, look, what I would have done (example):

echo '<pre>';
$categoryIds = array(1,2,3,4,5);
$collection = Mage::getModel('catalog/category')->getCollection();
    ->addFieldToFilter('entity_id', array('in', $categoryIds));
foreach ($collection as $category) {
    echo $category->getName() . PHP_EOL;

There are could be mistakes but my idea following:

  1. get the category collection
  2. add filtering by your IDs
  3. load it and get category models.

Or another scenario (not so fast)

  1. "foreach" category IDs
  2. and in "each" get new category object instance via Mage::getModel('catalog/category')->load($yourCategoryId)

it simple issue you need load Model factory class for each times.

// add this



 $category = $collection->load($cat);
  • reloading the collection every time seems to work, however, it isn't very efficient. Thanks though! Jan 6, 2015 at 20:13

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.