Magento - 当 php BodyClass == ‘whatever’ 时调用静态块
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您甚至不需要执行任何开发来在特定类别页面或任何基于 cms 或布局的页面上显示静态块。使用 Magento 的小部件功能。
转到“CMS -> 小部件”
按“添加新的小部件实例”按钮
选择小部件类型(在您的情况下为“CMS 静态块”)和应显示它的主题。按“继续”按钮。
输入“Widget Instance Title”并选择应显示其的商店。
按“添加布局更新”按钮并选择您的小部件“显示于”设置(类别、产品、CMS 页面、结账页面等),然后选择您想要在页面的哪个部分显示它。
转到“窗口小部件选项”选项卡并选择您要显示的静态块。
希望它能帮助您解决您的问题,而无需任何开发:)
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.
Go to "CMS -> Widgets"
Press "Add New Widget Instance" button
Select widget type (in your case it is "CMS Static Block") and theme where it should be shown. Press "Continue" button.
Type in "Widget Instance Title" and select stores where it should be displayed.
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.
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 :)
不太漂亮,但如果您将静态块命名为与页面名称相同,则此方法有效:
not pretty, but this works if you name the static blocks the same as your page name:
在您的
app/design/frontend/default//catalog/catalog/category/view.phtml
中尝试一下:在其中抛出一些
if
语句来检查null,你应该离开。干杯,
京东
try this inside your
app/design/frontend/default/<theme>/catalog/catalog/category/view.phtml
:Throw some
if
statements in there to check for nulls and you should be away.Cheers,
JD
在
2col-inside.phtml
中将其添加到您想要横幅的位置。这是安全的,因为如果
banner_id
不存在,则不会回显任何内容。对于您想要横幅编辑的每个类别,其“自定义布局更新”框包含以下内容...
...根据需要更改
block_id
。在产品和 CMS 页面上也可以执行相同的操作。如果页面未使用 2cols-inside.phtml 模板,则不会发生任何更改。In your
2col-inside.phtml
add this where you want the banner.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...
...changing the
block_id
as appropriate. The same can also be done on product and CMS pages. If a page is not using2cols-inside.phtml
template then nothing happen is changed.