我正试图从随机菜单中排除一个特定类别。我一直在寻找答案,并在这里找到了一些潜在的解决方案,但都没有奏效。
这就是我目前拥有的:
<?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; ?>
您的集合调用看起来非常夸张,原因我肯定对您有意义,所以我会这样做——如果您只想排除该类别,请将其添加到foreach;
<?php foreach ($categories as $category): ?>
<?php if($category->getId() !== $catIdEx) { ?>
<div>
<!-- your other code here -->
</div>
<?php } ?>
<?php endforeach; ?>
$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; ?>