My status

07 3103 2894

Adding Image rotator PHP code to Magento PDF Print E-mail
Written by Paul Whipp   
Friday, 18 June 2010 03:04

If you want to display something relating to products or files that is not covered by an existing Magento block, you may need to write some PHP to do it. In my case I needed an image rotator to cycle through some images. There are plenty of examples of rotators on the web and a few hints on doing it with Magento but I wanted control and I wanted to understand it so...

The PHP will need to go into my own block template. This is the phtml file that contains the php scrpt to generate the html. Its going to rotate images but I'll start with a very simple php file that just verifies its running PHP:

<?php echo '<p>Hello from image_rotator.phtml</p>' ?>

This goes into my image_rotator.phtml file which I now need to hook into Magento.

I need to put the file somewhere sensible in my theme so that the standard Magento block stuff can find it. Its a template so it goes under design/frontend/default/<my_theme>/template and, as I'm using it on my cms pages I'll put it in the cms folder:

design/frontend/default/<my_theme>/template/cms/image_rotator.phtml

Now, to get Magento to use it, I need to insert a block into a cms page. The type of the block is core/template because I'm doing all the work in my phtml file. If I was accessing Magento data or doing other 'model' stuff I'd use or create a block file to do that and that would then specify my block type. I insert:

{{block type="core/template" template="cms/image_rotator.phtml"}}

This can go into the top of my home page (or on any appropriate test page).

Now, when I fetch the page I can see my test code. We can now extend the PHP template file to do whatever we need.

For this example, I am going to make it rotate images. I wont dwell on the rotator code, I modified it from this image rotator which is nice and clean.

If you want to give this a go, everything you need is in this tar.

  1. Extract image_rotator_files.tar.gz in the root of your site
  2. Move the image_rotator.phtml file to design/frontend/default/<your_theme>/template/cms/image_rotator.phtml
  3. Insert the block statement shown above into a suitable CMS page
  4. Change the images in media/rotator_images to suit
  5. Watch the result

Perhaps if I get time I'll make this into a Magento extension with admin facilities to manage the images, associated links etc.

Trackback(0)
Comments (4)Add Comment
0
Can't get it to work
written by Wes Davis, June 23, 2010
Thanks for the write up. I have downloaded your files, and insterted them in the necessary positions.

When I add the block in the CMS page, it removes the content of the page.

Is the block supposed to go into the 'Content' of the CMS page or somewhere else?

Cheers, Wes
0
Actually..
written by Wes Davis, June 23, 2010
I just tested that is not the problem. i can echo some text on the page.

So there is some issue for me in the actual code for image_rotator.phtml

I am using Magento ver. 1.4.0.1

Will keep digging.
62
This was tested in Magento version 1.4.1.0 but should be flexible
written by Paul Whipp, June 24, 2010
This solution was tested in 1.4.1.0 but it should work fine in 1.4.x

If the content of the page is disappearing there is some error occurring that is breaking the presentation.

Given that getting image_rotator to just echo text is working, my best guess is that the file permissions or locations for the images are incorrect.
0
Thanks
written by Wes Davis, June 28, 2010
Thanks for the response Paul. Permissions was the next thing I checked, and everything is fine on that front.. including ownership.

I have just reverted to hardcoding the names of images and will have a re-visit later to see if I can get this to work for me.

Thanks again for the response mate smilies/smiley.gif

Write comment

security code
Write the displayed characters


busy
Last Updated on Friday, 18 June 2010 03:08
 
We have 19 guests online
Paul Whipp Consulting (BN 207 530 56) is owned by and operated for The Whipp Family Trust (ABN 32 507 522 641).