Magento Themes and Magento Extensions

Magento: Display Static Block via Custom Layout Update

Share
Posted on June 20th, 2011 | Posted by admin

We were needing to display a static block in the left column (of the 2-columns-left template). We only needed to display it for a few specific products though. There are a couple ways you can do it via Custom Layout Updates. In our case, the products were all in a specific category, and we wanted the static block to show up on the list view AND the product view. So, we put this code in on the product level and the category level:

<reference name="left">
    <block type="cms/block" name="static_block_identifier" before="-">
        <action method="setBlockId"><block_id>static_block_identifier</block_id></action>
    </block>
</reference>

Magento: Add Static Block to CMS Page

Share
Posted on May 30th, 2011 | Posted by admin

There is definitely nothing complicated about doing this. Static blocks are a great way to divide up different types of data blocks to make updating content easier.

There are just two simple steps to adding a static block to a cms page. First, create your static block. You will use the identifier to call it on the cms page. Let’s say, for this example, we call the identifier “home-page-promo”.

Once your static block is created, edit whatever cms page you would like this block to appear on, and simply put this code in right where you want it to show up:

{{block type="cms/block" block_id="home-page-promo"}}

And that’s all there is to it.

Simple random banner rotator in Magento using static blocks

Share
Posted on May 19th, 2011 | Posted by admin

This is my first post here and I’ll write about my first challenge regarding Magento since I came to work at Inchoo.

As title says I’ll show you how to implement simple random banner rotation functionality in Magento using static blocks.

First of all, upload images you want to use in “/skin/frontend/YOUR_INTERFACE/YOUR_THEME/images/

In admin panel add N static blocks (“CMS->Static Blocks->Add new block”)

For identifier field use something like this:

1
2
3
4
5
SomeBannerIdentifierName_1
SomeBannerIdentifierName_2
SomeBannerIdentifierName_3
...
SomeBannerIdentifierName_N

For each content use this:

1
<a href="ADDRESS_YOU_WANT_TO_USE"><img src="{{skin url=images/media/IMAGE_YOU_WANT_TO_USE.jpg}}" alt="Some alt text" /></a>

Please note that you are not restricted only to images, you could use text, video or whatever you want here, but I’ll focus on images with links as title says.

Now when all is set, make new file called banner_rotator.phtml in  “/app/design/frontend/YOUR_INTERFACE/YOUR_THEME/template/callouts/banner_rotator.phtml

And use this code inside:

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

*Make sure you replace “N” with highest number of your static block

In order to show this block, you should be familiar with Magento layouts.
Since that is out of scope for this article, I’ll show you how to put it below the content on cms pages.

Open “/app/design/frontend/YOUR_INTERFACE/YOUR_THEME/layout/cms.xml

[*] Put this line:

1
<block type="core/template" name="home.banner.rotator" template="callouts/banner_rotator.phtml"/>

Find this:

1
2
3
4
5
6
7
8
<cms_page>
<reference name="content">
<block type="cms/page" name="cms_page"/>
P U T  [*] H E R E  ! ! !
</reference>
</cms_page>

Unfortunately I can’t be more specific so feel free to ask me anything.

UPDATE:

If you want this to work for specific page such as homepage do this:

Instead of here /app/design/frontend/YOUR_INTERFACE/YOUR_THEME/layout/cms.xml
put this:

1
2
3
<reference code="content">
<block type="core/template" name="home.banner.rotator" template="callouts/banner_rotator.phtml"/>
</reference>

into your Admin panel’s CMS->Pages->Your page->Design->Layout Update XML field
Everything else is the same…

Page 2 of 2«12