robert
robert

Reputation: 1

Magento - call static block when php BodyClass == ‘whatever’

in my 2col-inside.phtml file i can successfully call a block using: <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('blue-banner')->toHtml() ?>

however i would like to serve up a unique banner based on the URI or body class. so something like:

<?php if($bodyClass['category-blue']): ?>  
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('blue-banner')->toHtml() ?>
<?php elseif($bodyClass['category-red']): ?>  
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('red-banner')->toHtml() ?> 
<?php else($bodyClass['category-yellow']): ?>  
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('yellow-banner')->toHtml() ?>  
<?php endif;?>

Setting the BlockId to the page url would work too (i'd just have to go back and rename all the blocks to exactly match the page url), but i don't know how to extract JUST the page url either.

Any suggestions?

Upvotes: 0

Views: 1479

Answers (4)

Ivan Chepurnyi
Ivan Chepurnyi

Reputation: 9233

You even don't need to perform any development for displaying the static block on a particular category page or any cms or layout based page. Use Widgets functionality of Magento.

  1. Go to "CMS -> Widgets"

  2. Press "Add New Widget Instance" button

  3. Select widget type (in your case it is "CMS Static Block") and theme where it should be shown. Press "Continue" button.

  4. Type in "Widget Instance Title" and select stores where it should be displayed.

  5. Press "Add Layout Update" button and select your widget "display on" settings (Categories, Products, CMS Pages, Checkout Pages, etc) and select in wich part of the page you want to show it up.

  6. Go to "Widget Options" tab and select static block which you want to show up.

Hope it will help you with your problem without any development :)

Upvotes: 1

clockworkgeek
clockworkgeek

Reputation: 37700

In your 2col-inside.phtml add this where you want the banner.

<?php echo $this->getChildHtml('banner_block') ?>

This is safe because nothing will be echoed if banner_id doesn't exist.

For each category where you want the banner edit it's "Custom Layout Update" box to include the following...

<reference name="content">
    <block type="cms/block" name="banner_block">
        <action method="setBlockId"><block_id>blue_banner</block_id></action>
    </block>
</reference>

...changing the block_id as appropriate. The same can also be done on product and CMS pages. If a page is not using 2cols-inside.phtml template then nothing happen is changed.

Upvotes: 0

Jonathan Day
Jonathan Day

Reputation: 18692

try this inside your app/design/frontend/default/<theme>/catalog/catalog/category/view.phtml:

$cat_name = $this->getCurrentCategory()->getName();
$block_name = $cat_name.'-banner';  
echo $this->getLayout()->createBlock('cms/block')->setBlockId($block_name)->toHtml();

Throw some if statements in there to check for nulls and you should be away.

Cheers, JD

Upvotes: 0

robert
robert

Reputation: 1

not pretty, but this works if you name the static blocks the same as your page name:

<?php   $_base_url = $this->helper('core/url')->getHomeUrl();
        $class = str_replace($_base_url, '', $this->helper('core/url')->getCurrentUrl());
        $pagetitle = str_replace('.html', '', $class);
        $page = str_replace('industries/', '', $pagetitle);
?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($page)->toHtml() ?>

Upvotes: 0

Related Questions