2012-09-20 37 views
0

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

我想添加一个函数,说如果子类超过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()); 

} 
} 
?> 

回答

1
if (count($subcats) > 0) 

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

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

工作就像一个魅力:) – Matt

0

也许你应该这样做:

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

之前:

if (count($subcats) > 0) 

因为你的$ subcats似乎是一个CSV字符串,然后更换您:

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

通过:

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