【问题标题】:Can I display Categories On Homepage using a template in magento?我可以使用 magento 中的模板在主页上显示类别吗?
【发布时间】:2011-10-22 12:38:11
【问题描述】:

我创建了一个简单的自定义模块,在主页上显示我的类别,为此我在我的自定义模块中创建了一个模型文件和一个块文件。并在我的 CMS(主页)中输入了这一行

{{block type="Definitivemagento_Helloword/randomproducts"}}

现在我希望使用模板(Phtml 文件和 Xml 文件)做同样的事情,因为我想添加一些我想用 Phtml 文件做的 css 和设计部分。有可能吗..这是我当前的模型类和块类代码。

class Pragtech_Sweet_Block_Category extends Mage_Core_Block_Template
{
    protected function _toHtml()
    {   $catModel = Mage::getModel('Pragtech_Sweet/category');
        $myCategory = $catModel->getCategory();
        $html = "<ul>";
        foreach ($myCategory as $category)
        {
        $name = $category->getName();

        $categoryLink = this->helper('catalog/category')->getCategoryUrl($category);
        $html .= "
        <p>
        <a href='$categoryLink'>$name</a><br/>
        <!-- $name <br/> -->
        </p>";
        }
        $html .= "<ul>";
        //echo "<pre>"; print_r($html); exit;
        return $html;
    }
}

我的模型类是:-

Class Pragtech_Sweet_Model_Category extends Mage_Core_Model_Abstract
{
    public function getCategory()
    {
        $categoryCollection = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('name');

        $categoryCollection->getSelect()->order(entity_id);
        //print_r($categoryCollection); exit;
        return $categoryCollection;
    }

【问题讨论】:

    标签: magento-1.4 magento-1.5 magento


    【解决方案1】:

    您可以使用类似的东西从另一个 phtml 块模板呈现任意块

    <?php echo $this->getLayout()->createBlock('definitivemagento_helloword/randomproducts')->toHtml(); ?>
    

    【讨论】:

      【解决方案2】:
      <div class="categories">
      <ul>
      <?php
      foreach ($this->getStoreCategories() as $_category): ?>
        <?php echo $this->drawItem($_category) ?>
        <?php endforeach; ?>
      </ul>
      </div>
      

      在您所需的任何 phtml 文件中添加此脚本并调用它,将显示所有类别及其各自的子类别。
      注意:您应该已经从管理面板添加了类别。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-11
        • 1970-01-01
        相关资源
        最近更新 更多