n00bly n00bly - 4 months ago 37
PHP Question

Magento category description on frontpage

I have a Magento shop and want to display the top-categories with description and image on the frontpage. Does anyone know how i can fix this?

I have this to display the top-categories:

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>


But i can't get it showing also the description and image.
Hope someone can help me with this.

Regards,
Robert

Answer

You can achieve this using module:

Note: In this approach, you can display the block where you want to display(Home page, footer, sidebar, other pages).

Steps: Here, [Namespace]=Codilar & [ModuleName]=Category

Create xml : app/etc/modules/Codilar_Category.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Codilar_Category>
      <active>true</active>
      <codePool>local</codePool>
    </Codilar_Category>
  </modules>
</config>

Create config.xml: app/code/local/Codilar/Category/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Codilar_Category>
      <version>1.0</version>
    </Codilar_Category>
  </modules>
  <global>
    <blocks>
      <codilar_category>
        <class>Codilar_Category_Block</class>
      </codilar_category>
    </blocks>
    <models>
      <codilar_category>
        <class>Codilar_Category_Model</class>
      </codilar_category>
    </models>
  </global>
</config>

Create Model: app/code/local/Codilar/Category/Model/Category.php

<?php
class Codilar_Category_Model_Category extends Mage_Core_Model_Abstract {
public function getActiveCategory() {
    $categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addIsActiveFilter();
    return $categories;
  }
}

Create Block: app/code/local/Codilar/Category/Block/Category.php

 <?php
    class Codilar_Category_Block_Category extends Mage_Core_Block_Template {
      public function getActiveCategory() {
        $arr_categories = array();
        $categories = Mage::getModel("codilar_category/category")->getActiveCategory();
        foreach ($categories as $category) {
          $arr_categories[] = array(
            'id' => $category->getId(),
            'name' => $category->getName(),
            'url' => $category->getProductUrl(),
            'image' => $category->getThumbnail(),
            'desc' => $category->getDescription(),
          );
        }
        return $arr_categories;
      }
    }

Create template file: app/design/frontend/default/default/template/codilar/category/category.phtml

    <?php
    $categories = $this->getActiveCategory();
    ?>
    <div id="category_list">
      <h1>All Category</h1>
      <?php if (is_array($categories) && count($categories)) { ?>
        <?php foreach($categories as $category) { ?>
          <div>
            <a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
          </div>
          <div>
            <img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'. $category['image']; ?>" alt="<?php echo $category['image'] ?>" height="100" width="100">
          </div>
        <div>
        <p><?php echo $category['desc'] ?></p>
      </div>
        <?php } ?>
      <?php } ?>
    </div>

To insert your category custom block using the layout update file, you can use the following code.

<block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"></block>

On the other hand, if you want to display your category custom block using the CMS page, here is the code you should use.

{{block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"}}