duanhe6464 2016-07-26 05:44
浏览 47
已采纳

首页上的Magento类别说明

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

  • 写回答

2条回答 默认 最新

  • drbe16008 2016-07-26 08:29
    关注

    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"}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?