Showing posts with label display category in homepage. Show all posts
Showing posts with label display category in homepage. Show all posts

Tuesday, April 24, 2012

Magento add category with images on homepage



if you would like to add category titles with images on the front page, just follow the steps:

place this snippet on your homepage cms page:

{{block type="core/template" name="homepage" template="catalog/category/homepagecategory.phtml"}}

then place this code in app/code/design/frontend/YOUR_PACKAGE/YOUR_TEMPLATE/catalog/category/homepagecategory.phtml

<?php
 $currcatId = 3;
 if($currcatId != NULL)
    {
  $collection = Mage::getModel('catalog/category')->getCategories($currcatId);
  $ctr = 0;
  foreach($collection as $subcat)
  {
      //limits the categories to be shown on home page to 6 categories
      if ($ctr < 6)
   if($subcat->getIsActive())
   {
       $category = Mage::getModel('catalog/category')->load($subcat->getEntityId());
       $ctr++;
       if (strtolower($category->getName()) == "overige lampen") 
       {
       }
    else
   {
?>

<div class="sub-category-container" style="margin-right:<?php if(($ctr % 2) == 0) {echo "0";} else {echo "8px";} ?>; margin-bottom:10px;" >
 <a href="<?php echo $this->getBaseUrl() . $category->getUrlKey() ?>" style="background:none; border:none;">
  <div>
     <?php if($_imgUrl = $category->getImageUrl()): ?>
   <img src="<?php echo $_imgUrl; ?>" border="0" />
     <?php else:?>
   <img src="<?php echo $this->getSkinUrl('images/lampen/placeholder.jpg') ?>" border="0" width="185px" height="185px" />
     <?php endif; ?>
  </div>
  <div>
   <a href="<?php echo $this->getBaseUrl() . $category->getUrlKey() ?>"><?php echo $category->getName(); ?></a>
  </div>
 </a>
</div>

<?php
     }
   }
  }
 }
?>

Tuesday, November 23, 2010

Magento - Display Categories (with category images) in the homepage

<div class="top-home-category">
<?php 
/******** Don't know why I commented this
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';

if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
****************************************/
?>

<?php // Iterate all categories in store
 $limit = 0;
$_helper    = $this->helper('catalog/output');
foreach ($this->getStoreCategories() as $_category): 
// If category is Active
if($_category->getIsActive()):
// Load the actual category object for this category
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
if ($_imgUrl = $cur_category->getImageUrl()){
$_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
$_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
} 
?>
<div class="home-category">
<div class="linkimage">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
<?php echo $_imgHtml; ?>
</a>
</div>
<div class="category-link-container">
<a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
<?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
</a>
</div>
</div>

// Load a random product from this category
/*$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->addAttributeToSelect('small_image');
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
$products->load();
// This a bit of a fudge - there's only one element in the collection
$_product = null;
foreach ( $products as $_product ) {}
?>
<div class="home-category">
<div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>">
<?php
if(isset($_product)):
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<?php
endif;
?> </a></p>
</div>
<a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a>
</div>
*/?> <?php
$limit+=1;
endif;
if ($limit==8):
break;
endif;
endforeach;
?> 
</div>

Thursday, September 9, 2010

Display Categories in the Home page

  • Display Categories in the Home page
  • Display a random product's picture with the category name
  • It can only fetch the 1st level category

-----------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN CATALOG/CATEGORY/LIST.PHTML (create it if not present) */

<?php // Iterate all categories in store
    $limit = 0;
    $_helper    = $this->helper('catalog/output');
    foreach ($this->getStoreCategories() as $_category):

        // If category is Active
        if($_category->getIsActive()):


            // Load the actual category object for this category
            $cur_category = Mage::getModel('catalog/category')->load($_category->getId());

            if ($_imgUrl = $cur_category->getImageUrl()){

                $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($cur_category->getName()).'" title="'.$this->htmlEscape($cur_category->getName()).'"  width="105px" />';
                $_imgHtml = $_helper->categoryAttribute($cur_category, $_imgHtml, 'image');
            } ?>
             <div class="home-category">
                <div class="linkimage">
                    <a href="<?php echo $this->getCategoryUrl($cur_category) ?>" style="border:none">
                    <?php echo $_imgHtml; ?>
                    </a>
                </div>
                <div class="category-link-container">
                <a href="<?php echo $this->getCategoryUrl($cur_category) ?>">
                       <?php echo $_helper->categoryAttribute($cur_category, $cur_category->getName(), 'name') ?>
                </a>
                </div>
            </div>
<?php
           $limit+=1;
        endif;

        if ($limit==8):
            break;
        endif;

    endforeach;
?>

----------------------------------------------------------------------------------------------------------
/* PUT THIS CODE IN THE CMS/PAGES/HOME*/
/* IF YOU WANT TO HARDCODE IT PLACE THIS IN LAYOUT/PAGE.XML -> "Custom page layout handles" AREA */

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}
----------------------------------------------------------------------------------------------------------


Reference