0

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.

Thanks

1

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:

<?php 
$images = array('img1.png', 'img2.png', ....);
shuffle($images);
$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" />
</reference>
0

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', ));

  $form->getElement('filenamelogo')->setRenderer(Mage::app()->getLayout()->createBlock('module/adminhtml_module_edit_renderer_logo')); 

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;
}}
0

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

Like:

BannerIdentifier_1

BannerIdentifier_2

BannerIdentifier_3

BannerIdentifier_4

...

BannerIdentifier_N

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, privacy policy and cookie policy

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