【问题标题】:Magento 1.7 If Home Page Condition in .phtmlMagento 1.7如果.phtml中的主页条件
【发布时间】:2015-07-20 19:31:44
【问题描述】:

我正在使用 magento 1.7.0.2,我遇到了一个问题并用谷歌搜索了所有解决方案,但对我不起作用...

我的问题是我想在我的 2Column-left.phtml 布局中添加 If 条件。 比如,如果我在主页上,我想显示此内容

                **<div><?php echo $this->getChildHtml('sectionA') ?></div>**

                <div class="product-content section-content-2">
                    <div class="container">
                        <div class="col-left sidebar">
                            <?php echo $this->getChildHtml('left') ?>
                        </div>
                        <?php echo $this->getChildHtml('content') ?>
                    </div>
                </div>

如果我不在主页上:

                <div class="product-content section-content-2">
                    <div class="container">
                        <div class="col-left sidebar">
                            <?php echo $this->getChildHtml('left') ?>
                        </div>
                        <?php echo $this->getChildHtml('content') ?>


                **<div><?php echo $this->getChildHtml('sectionA') ?></div>**

                    </div>
                </div>

我使用了所有解决方案,例如:

http://dltr.org/blog/magento/187/Magento-How-to-find-if-you-are-on-homepage-getIsHomePage

Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled

但不工作...请帮助并建议我应该做什么或我是否犯了错误???

谢谢

【问题讨论】:

    标签: php magento magento-1.7


    【解决方案1】:
     $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if($action=='cms_index_index'){
        echo "home";?>
    
        <div><?php echo $this->getChildHtml('sectionA') ?></div>**
    <?php } ?>
    
                    <div class="product-content section-content-2">
                        <div class="container">
                            <div class="col-left sidebar">
                                <?php echo $this->getChildHtml('left') ?>
                            </div>
                            <?php echo $this->getChildHtml('content') ?>
                        </div>
                    </div>
    

    非主页:

    <div class="product-content section-content-2">
                        <div class="container">
                            <div class="col-left sidebar">
                                <?php echo $this->getChildHtml('left') ?>
                            </div>
                            <?php echo $this->getChildHtml('content') ?>
    
    <?php 
    $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if($action!='cms_index_index'){ ?>
    <div><?php echo $this->getChildHtml('sectionA') ?></div>
    <?php } ?>
    
                        </div>
                    </div>
    

    【讨论】:

    • 您好阿米特,感谢您的回答。这可以是 if 或 else 条件吗?我只想根据如果主页重新排序我的部分,否则......
    • 不工作,它显示此错误“解析错误:语法错误,C:\wamp\www\flavours\app\design\frontend\flavours\default\template\page 中的意外'}' \2columns-left.phtml 第 60 行“
    【解决方案2】:

    在头 phtml 文件中:

    if($this->getIsHomePage()) {
        echo 'You are in Homepage!';
    } else {
        echo 'You are NOT in Homepage!';
    }
    

    在其他 phtml 文件中:

     if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()) {
                echo 'You are in Homepage!';
            } else {
                echo 'You are NOT in Homepage!';
            }
    

    【讨论】:

      【解决方案3】:

      您可能想看看 Magento 是如何处理这个问题的。 Mage_Page_Block_Html_Header 类中的第 46 行显示:

       /**
       * Check if current url is url for home page
       *
       * @return true
       */
      public function getIsHomePage()
      {
          return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 1970-01-01
        • 1970-01-01
        • 2012-09-05
        • 1970-01-01
        相关资源
        最近更新 更多