[英]How to display magento product categories in column wise
I'm trying to display all the categories below the main title, so I want to display four categories in each column. 我试图在主标题下显示所有类别,所以我想在每列中显示四个类别。 The below is my code 下面是我的代码
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul class="pull-left">
<?php foreach ($collection as $cat):?>
<li>
<a style="color:#fff;" href="<?php echo $helper->getCategoryUrl($cat);?>">
<cite><?php echo $cat->getName();?><?php
?></cite>
</a>
</li>
<?php endforeach;?>
</ul>
HTML Output HTML输出
<ul class="pull-left">
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
.
.
.
.
</ul>
I want to define four li
items in every ul
. 我想在每个ul
定义四个li
项。 Like this 像这样
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
I also want to display product count and I've tried this 我也想显示产品数量,我已经尝试过了
<?php
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
?>
but it is not working. 但它不起作用。
I am using this code on my category page and it is giving me count. 我在我的类别页面上使用了此代码,这很有价值。 Please have a look at the code I have added before tag. 请查看我在标记之前添加的代码。
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a></br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()); echo"<br>";
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($subCategoryId)->getProductCollection()->getSize();
echo'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
Please use this one. 请使用这个。
You can adjust ul ,li as per wish. 您可以根据需要调整ul,li。 Right now this will list four subcategories in a single row. 现在,这将在一行中列出四个子类别。
This code will give you the subcategories of a parent category. 此代码将为您提供父类别的子类别。
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a></br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()).'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
</ul>
Please use this code. 请使用此代码。 This will surely work for you. 这肯定会为您工作。
<?php
$category = Mage::getModel('catalog/category')->load($categoryID);
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($category)->getProductCollection()->getSize();
?>
Thanks, 谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.