Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:04:53+00:00 2026-06-09T21:04:53+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:04:54+00:00Added an answer on June 9, 2026 at 9:04 pm

    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()');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code rite now that just generates a random drink combination from
I need to display the parent from the first Element of input.xml Also I
I have an app that will display Lotto Max numbers, I need to make
I need to display 3 random images from SQLite database horizontally. Since it's impossible
I have a bunch of images at random heights and widths that need to
I have a random mysql response. I need to display the response in smarty
I have a game that will display ten random cards based on number and
I need to be able to display a set of 1200 random combinations out
I need to display all the employees in one webpart. I created the gridview
I want to display 1 random record from a database based on the week.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.