如何优化图像加载所有类别


8

现在,我有两个嵌套的foreach可以遍历所有类别,而且速度非常慢(本地服务器上为3-4秒)。我认为问题是我使用-> LOAD()来获取子类别的图像路径。还有其他方法吗?还是我应该重新设计整个导航ui?有什么建议吗?

$categories = Mage::getSingleton('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('id')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('url')
    ->addAttributeToFilter('level',2)
    ->addIsActiveFilter()
    ->addAttributeToSort('position');

foreach ($categories as $category): 
    $id = $category->getId();
    $children = $category->getChildrenCategories();
    $name = $category->getName();
    $url_path = $category->getUrl();        

    foreach ($children as $child):                                      
        $childId = $child->getId();
        $thisChild = Mage::getSingleton('catalog/category')->load($childId);
        $name = $thisChild->getName();
        $url_key = $thisChild->getUrlKey(); 
        $url_path = $child->getUrl();
        $img = $thisChild->getImageUrl();   
    endforeach;
endforeach;

Answers:


3

我不知道这是否有效,但是您可以尝试使用 Mage_Catalog_Model_Resource_Category_Tree

此类应使您可以遍历树,但在加载所有类别之后。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.