I want to create a static block which shows randomly one image from a specified image folder. Does anyone know if this is possible and how to create it.


3 Answers 3


Create a template file.
Let's call it random.phtml.
Put it in app/design/frontend/base/default/template/random/random.phtml.

The contents should look something like this:

$images = array('img1.png', 'img2.png', ....);
$image = $images[0];
$url = Mage::getBaseUrl('media').'path/to/folder/'.$image;  
<img src="<?php echo $url?>" alt="" />

make sure you put all your images in media/path/to/folder. (feel free to change the name of the folder).

Now you can use this in 3 ways.

Either inside a static block:

{{block type="core/template" template="random/random.phtml"}}

either in any other template:

$block = Mage::app()->getLayout()->createBlock('core/template')->setTemplate('random/random.phtml');
$html = $block->toHtml();
echo $html;

either in a layout file:

<reference name="left"><!-- not necessarily left. can be right, content, header... -->
    <block type="core/template" template="random/random.phtml" name="random-image" as="random-image" />

If you want to create static block in your form steps:

1) go to -> Namespace_Module_Block_Adminhtml_Module_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form

Add code : $fieldset->addField('filenamelogo', 'text', array( 'name' => 'filenamelogo', ));


2) Create a file with name Logo.php on Block/Adminhtml/Module/Edit/Renderer/Logo

3) Add code in Logo.php class Namespace_Module_Block_Adminhtml_Merchant_Edit_Renderer_Logo extends Mage_Adminhtml_Block_Widget implements Varien_Data_Form_Element_Renderer_Interface {

public function render(Varien_Data_Form_Element_Abstract $element) 
{ $html .= ' your html or php code '
return $html;

You need to create multiple static blocks, with a mutual identifier prefix:








Then pass your image to whatever block:

<a href="http://mystore.com/my_new_product/"><img src="{{media url="wysiwyg/Banner/ban_1.PNG"}}" alt="Some alt text"  /></a>

Then call that static block on phtml file:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('BannerIdentifier_'.mt_rand(1, N))->toHtml() ?>

I am not sure if this is best, but it can be a way of doing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.