Magento - 当 php BodyClass == ‘whatever’ 时调用静态块

发布于 2024-09-30 12:23:22 字数 857 浏览 2 评论 0原文

在我的 2col-inside.phtml 文件中,我可以使用以下命令成功调用块: getLayout()->createBlock('cms/block')->setBlockId('blue-banner')->toHtml() ?>

但是我想提供一个基于 URI 或正文类的独特横幅。所以类似:

<?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;?>

将 BlockId 设置为页面 url 也可以(我只需返回并重命名所有块以与页面 url 完全匹配),但我也不知道如何提取页面 url 。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白首有我共你 2024-10-07 12:23:22

您甚至不需要执行任何开发来在特定类别页面或任何基于 cms 或布局的页面上显示静态块。使用 Magento 的小部件功能。

  1. 转到“CMS -> 小部件”

  2. 小部件”

    按“添加新的小部件实例”按钮

  3. 选择小部件类型(在您的情况下为“CMS 静态块”)和应显示它的主题。按“继续”按钮。

  4. 输入“Widget Instance Title”并选择应显示其的商店。

  5. 按“添加布局更新”按钮并选择您的小部件“显示于”设置(类别、产品、CMS 页面、结账页面等),然后选择您想要在页面的哪个部分显示它。

  6. 转到“窗口小部件选项”选项卡并选择您要显示的静态块。

希望它能帮助您解决您的问题,而无需任何开发:)

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 :)

梦初启 2024-10-07 12:23:22

不太漂亮,但如果您将静态块命名为与页面名称相同,则此方法有效:

<?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() ?>

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() ?>
梦回旧景 2024-10-07 12:23:22

在您的 app/design/frontend/default//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();

在其中抛出一些 if 语句来检查null,你应该离开。

干杯,
京东

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

尐籹人 2024-10-07 12:23:22

2col-inside.phtml 中将其添加到您想要横幅的位置。

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

这是安全的,因为如果 banner_id 不存在,则不会回显任何内容。

对于您想要横幅编辑的每个类别,其“自定义布局更新”框包含以下内容...

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

...根据需要更改 block_id。在产品和 CMS 页面上也可以执行相同的操作。如果页面未使用 2cols-inside.phtml 模板,则不会发生任何更改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文