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.
Traci Kaspar
12 May. 2010
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
ben@sonassi.com
12 May. 2010
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
14 Jun. 2010
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
ben@sonassi.com
26 Jul. 2010
Hi Robert,
You would be better off just using a standard
core/templatecall to a custom template withscan_dirand reduce the array to 5 items, then output withimplode()orforeachilnar
29 Jul. 2011
Hi,
You’re shuffling whole array just to select one random element. Very nice approach..
Benjamin
29 Jul. 2011
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).