【问题标题】:If body has class then do this this etc如果身体有课,那么这样做等等
【发布时间】:2014-01-08 06:28:54
【问题描述】:

Magento 新手(wordpress 开发人员),我在 Magento 中为这个概念而苦苦挣扎,不知道如何使用他们的 api 来实现这一点,并设置 if body class= x 的变量

简而言之,如果正文有一个“x”类,那么就会发生这种情况(我正在尝试针对特定类,所以如果子页面有一个“其他”类,例如,那么它将运行一个 if或 elseif 用于该页面):

<?php if ($this->getLayout()->createBlock("page/html")->getBodyClass() == 'home'): ?>

//add this content

<?php elseif($this->getLayout()->createBlock("page/html")->getBodyClass() == 'options'):?>

//add different divs content etc

<?php else: ?>

//stuff

<?php endif; ?>

【问题讨论】:

  • 你需要身体等级的条件吗?还是需要为主页设置条件?
  • @Asifhhh 身体类的需要条件有一个与之匹配的类。所以我想可以这么说确实可行。对不起,我应该指定的。我会更新的。
  • @ravipatel 我删除了那个帖子,仍然可以兑现
  • 不通过重复问题您的问题已通过使用此代码解决。请在布局的有效位置使用它。

标签: php magento if-statement magento-1.7 magento-1.8


【解决方案1】:

可以通过css类获取

$this->getBodyClass()

用于块类型

  Mage_Page_Block_Html

否则

    $this->getLayout()->createBlock("page/html")->getBodyClass();

【讨论】:

  • 你会这样做吗:$class = $this-&gt;getBodyClass(); 那么我的 BODY CLASS 示例将是` if($class='sub'): ?> content 更多的东西`等等 ??`
  • 我正在尝试在模板中使用它,例如 header.phtml
  • 每个模板文件都有 $this 的作用域,所以你需要具体说明哪个模板文件?
  • 我的错,比如header.phtml,根据body class会有不同的内容
  • OK - 如果 $this 不起作用,则将 $this 替换为 $this->getLayout()->createBlock("page/html") 它将起作用
【解决方案2】:
<?php

        $body_classes = $this->getLayout()->createBlock("page/html")->getBodyClass();

        if($body_classes=='cms-index-index'):

            //your code
        elseif($body_classes=='cms-page-view'):
            //your code

        else:    
            //your code
        endif;
      ?>      

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多