douwen4401 2012-08-14 17:18
浏览 13
已采纳

Magento:显示随机子类别

I need to display 4 random subcategories from a parent category.

I have the following code, that displays ALL subcategories from the needed parent category (ID=4) with random product images: list.phtml \app\design\frontend\MYTHEME\default\template\catalog\category

<?php

// Iterate all categories in store foreach ($this->getChildCategories(4) as $_childCategory):

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

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


       // 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 style="float: left; padding-right: 30px; text-align: center;">
           <div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_childCategory)?>">

       <?php
       if(isset($_product)):
       ?>
       <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" alt="<?php echo $_childCategory->getName()?>" title="<?php echo $_childCategory->getName()?>" />
       <?php
       endif;
       ?>
           </div>
           <?php echo $_childCategory->getName()?></a></p>
       </div>
       <?php
   endif;

endforeach; ?>

and in navigation.php \app\code\local\Mage\Catalog\Block

public function getChildCategories($categoryId) //inserted for random subcategories on category page
    {
          $category = Mage::getModel('catalog/category');
          if($category->checkId($categoryId) === false) {
              return false;
          }
        $layer = Mage::getSingleton('catalog/layer');
        $category->load($categoryId);
        $layer->setCurrentCategory($category);
        /* @var $category Mage_Catalog_Model_Category */
        $collection = Mage::getModel('catalog/category')->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        $collection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
            ->joinUrlRewrite()
            ->load();

        //$productCollection = Mage::getResourceModel('catalog/product_collection');
        //$layer->prepareProductCollection($productCollection);
        //$productCollection->addCountToCategories($collection);
        return $collection;
    }

How can I limit the subcategories to 4 and display them randomly? Thanks!

  • 写回答

1条回答 默认 最新

  • dongziya9863 2012-08-14 18:22
    关注

    To limit a collection you can add the following:

    $collection->setPageSize(4);
    

    To randomize your returned records, is one of the possiblities:

    $collection->getSelect()
        ->order('rand()');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?