Magento random block generator / block randomiser

This could be pretty useful if you want to combine XML block control with a random content display (eg. banner ads).

Very easily achieved, simply create 1 file in ./base/default/template/page/html/random_block.phtml

 <?php 

  $children = $this->getSortedChildren();
  
  if(is_array($children)) {
    shuffle($children);
    echo $this->getChildHtml($children[0]); 
  }
 
 ?>

Then in your relevant XML file – control the output by using:

<block type="core/template" name="left.callout.random" template="page/html/random_block.phtml" before="-">
  <block type="core/template" name="left.callout.random1" template="callouts/left_col.phtml">
    <action method="setImgWidth"><width>240</width></action>
    <action method="setImgSrc"><src>images/image1.png</src></action>
    <action method="setImgAlt" translate="alt" module="catalog"><alt>some alt text</alt></action>
    <action method="setLinkUrl"><url>link url</url></action>
  </block>
  <block type="core/template" name="left.callout.random2" template="callouts/left_col.phtml">
    <action method="setImgWidth"><width>240</width></action>
    <action method="setImgSrc"><src>images/image2.png</src></action>
    <action method="setImgAlt" translate="alt" module="catalog"><alt>some alt text</alt></action>
    <action method="setLinkUrl"><url>link url</url></action>
  </block>
</block>

You can add any type of block you want and it will just grab one at random.

  • http://atlantel.net Traci Kaspar

    Thank you for this posting. Please forgive my ignorance – I created the random.phtml.

    I want to now place the code in the “relevant xml” file. How do I know what the relevant file is? Would it be catalog.xml ? Can I put this code in a cms block?

    What I would really like to do – is have a folder ie – banners – and have it randomly pick a banner from the folder everytime the page is refreshed…Will this do that? I will have up to 25 different banners.
    I am a graphics designer turned into a magento admin LOL. I have figured out many things – but until I can afford the Zend class to learn php – the manipulation of the code is slightly out of my comfort zone.

    Any help is appreciated!

    Traci Kaspar
    Creative Designer
    Atlantel
    404-969-5009

  • https://www.sonassi.com ben@sonassi.com

    Hi Traci,

    The relevant file would be the file for the module where you want the content to appear.

    cms.xml – for cms pages
    catalog.xml – for catalogue pages
    catalogsearch.xml – for search result pages
    etc.

    If you want it on all pages, then pop it in the tag at the top of any .xml file.

  • Robert

    Hi,

    Very nice piece of code!

    Only one question, how can i use this when i want to show for example 5 images?

    i have 1 folder full of images and want to show 5 at the SAME time but rondom from the whole folder.

    Hope you can help me.

    Thanks in advance!

    Greets, Robert

  • https://www.sonassi.com ben@sonassi.com

    Hi Robert,

    You would be better off just using a standard core/template call to a custom template with scan_dir and reduce the array to 5 items, then output with implode() or foreach

  • ilnar

    Hi,
    You’re shuffling whole array just to select one random element. Very nice approach..

  • https://www.sonassi.com Benjamin

    True, you could just use array_rand – but the original purpose for this code required a number of elements to be displayed (we’re just posting a code snippet here).