简体   繁体   English

Magento-如何在phtml中过滤类别

[英]Magento - how to filter categories in phtml

I have been working on the maga menu. 我一直在maga菜单上工作。 I got the collection in phtml like: 我在phtml中得到了如下集合:

<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php $currentCategory = Mage::registry('current_category') ?>

Now I have to add the filter to show specific categories. 现在,我必须添加过滤器以显示特定类别。 Like I have an array(1,2,3,4) of categories that i want to show. 就像我有一个要显示的类别数组(1,2,3,4) so how can I apply filter to this Helper. 因此,如何将过滤器应用于此帮助器。

Any one have any suggestion please answer. 任何人有任何建议请回答。

Thanks. 谢谢。

use this code 使用此代码

<?php $catids[]=array(1,2,3,4);

 foreach($catids as $id):

                        $_category = Mage::getModel('catalog/category')->load($id);

                     if($_category->getIsActive()):
                            echo $_category->getName();
                     endif;
endforeach;

?>

and don't forget to link my answer if it was helpful 如果有用的话,别忘了链接我的答案

The first answer is correct but it's not efficient as it consumes unnecessary database round trips. 第一个答案是正确的,但是效率不高,因为它消耗了不必要的数据库往返行程。 @Karan's code issues a query to the database for every id. @Karan的代码针对每个ID向数据库发出查询。 Just imagine if the number of category ids to be filtered were 50 or above. 试想一下,要过滤的类别ID的数量是否为50或以上。

My example would be this: 我的例子是这样的:

<?php

$catIds = array(1,2,3,4);

$catCollection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('id', $catIds)->addAttributeToFilter('is_active',1);

foreach($catCollection as $category){
  echo $category->getName()." ";
}

This will reduce the database roundtrip to just one. 这样会将数据库往返次数减少到仅一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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