仅在子类别大于1时显示Div

时间:2012-09-20 08:35:23

标签: php magento

我有一段代码,用于查找是否存在某个类别的子类别,如果有,则会在列表视图中显示子类别。

我试图添加一个函数,说明子类别是否超过1,然后显示DIV,如果没有,则不显示DIV,因为它不会显示任何子类别。

这是我到目前为止所做的,但它似乎没有起作用。

<?php 
/* Load category by id */
$cat = Mage::registry('current_category');

/*Returns comma separated ids*/
$subcats = $cat->getChildren();

if (count($subcats) > 0): ?>

<div style="width:100%; height:auto; text-align:center; font-size:16px; color:#373737; padding:2px 0; margin-top:-5px; margin-bottom:3px; background:#f1f1f1;">Choose Your Category</div>

<?php endif;

foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
  echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a></li>';

/* Load category by id */
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());

}
}
?> 

2 个答案:

答案 0 :(得分:1)

if (count($subcats) > 0) 

我相信你的$ subcats不是数组的字符串所以你应该使用strlen函数

if(strlen(trim($subcats)) > 0)

答案 1 :(得分:0)

也许你应该这样做:

$subcats = explode(',', $cat->getChildren());

之前:

if (count($subcats) > 0)

因为你的$ subcats似乎是一个csv字符串,然后替换你的:

foreach(explode(',',$subcats) as $subCatid)

通过:

foreach ($subcats as $subCatid)
相关问题