| Adding Image rotator PHP code to Magento |
|
|
|
| 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:
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.
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)
![]() 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. 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
Write comment
|
| Last Updated on Friday, 18 June 2010 03:08 |






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