Magento:从下拉菜单中排除特定类别

时间:2015-02-09 19:00:52

标签: php magento

我试图从随机菜单中排除一个特定类别。我已经搜索了答案,并在这里找到了一些可能的解决方案,但没有一个有效。

这就是我目前所拥有的:

<?php
    $catID = $this->category_id;
    $catName = $this->category_name;
    $catType = $this->category_type;
    $category = Mage::getModel('catalog/category')->load($catID);
    $categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'thumbnail', 'description'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter( $category->getChildren() );
    $catIdEx = 38;
    $categories->getSelect()->join(array('cats' => 'catalog_category_product'), 'cats.product_id = e.entity_id');
    $categories->getSelect()->where('cats.category_id', array('neq' => $catIdEx));
    $categories->getSelect()->group(array('e.entity_id'));
    $categories->getSelect()->order('RAND()');
    $categories->getSelect()->limit(1);
?>
<?php foreach ($categories as $category): ?>
<div>
    <a href="<?php echo $category->getUrl(); ?>">
        <img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" class="boxedimage" />
    </a>
    <br />
    <h3>Spotlight <?php echo $catName ?></h3>
    <?php
        $sdesc = $category->getDescription();
        $sdesc = trim($sdesc);
        $limit = 230;
        if (strlen($sdesc) > $limit) {
            $sdesc = substr($sdesc, 0, strrpos(substr($sdesc, 0, $limit), ' '));
        }
    ?>
    <?php echo $sdesc."..."; ?>
    <div><a href="<?php echo $category->getUrl(); ?>" class="go">View <?php echo $category->getName(); ?> <?php echo $catType ?>...</a></div>
</div>
<?php endforeach; ?>

2 个答案:

答案 0 :(得分:0)

你的收集电话看起来非常重要,因为我确定对你有意义,所以我会选择这个 - 如果你只想排除那个类别,请将其添加到foreach中;

<?php foreach ($categories as $category): ?>
<?php if($category->getId() !== $catIdEx) { ?>
<div>
<!-- your other code here -->

</div>
<?php } ?>
<?php endforeach; ?>

答案 1 :(得分:0)

$excluded_category = array('20','4','6');
<?php foreach ($categories as $category): ?>
<?php if(!in_array($category->getId(),$excluded_category)) { ?>
<div>
<!-- your other code here -->

</div>
<?php } ?>
<?php endforeach; ?>