2011-11-26 40 views
2

我正在嘗試做一些看起來應該很簡單的事情:在每頁的頂部鏈接上添加一個「主頁」鏈接,除了主頁(我正在使用cms頁面作爲我的主頁)。如果可能,我想完全在我的local.xml中完成。如何通過Magento 1.5.1中的local.xml有條件地刪除頂級主頁鏈接

我的想法是添加鏈接默認

<default> 
    <reference name="top.links"> 
     <action method="addLink" translate="label title"> 
      <label>Home</label> 
      <url>/</url> 
      <title>Home</title> 
      <prepare>true</prepare> 
      <urlParams helper="core/url/getHomeUrl"/> 
      <position>1</position> 
      <liParams/> 
      <aParams>class="top-link-home"</aParams> 
      <beforeText></beforeText> 
      <afterText>/</afterText> 
     </action> 
    </reference> 
</default> 

,然後刪除它cms_index_index

<cms_index_index> 
    <reference name="top.links"> 
     <action method="removeLinkByUrl"><url helper="core/url/getHomeUrl"/></action> 
    </reference> 
</cms_index_index> 

但這並沒有奏效,在主頁的鏈接處處顯示出來,包括主頁。

我在做什麼錯?有沒有另一種方法可以做到這一點,不涉及黑客攻擊?

編輯: 我拼寫出整個URL所期望的行爲,無論是在我的addLink

<default> 
    <reference name="top.links"> 
     <action method="addLink" translate="label title"> 
      <label>Home</label> 
      <url>http://www.mysite.com/</url> 
      <title>Home</title> 
      <prepare/> 
      <urlParams/> 
      <position>1</position> 
      <liParams/> 
      <aParams>class="top-link-home"</aParams> 
      <beforeText></beforeText> 
      <afterText>/</afterText> 
     </action> 
    </reference> 
</default> 

和我removeLinkByUrl

<cms_index_index> 
    <reference name="top.links"> 
     <action method="removeLinkByUrl"><url>http://www.mysite.com/</url></action> 
    </reference> 
</cms_index_index> 

解決了眼前的問題,但不回答我原來的問題。我想我需要更好地瞭解Magento如何使用助手呈現網址。

+0

問題來自Magento添加SID參數。它沒有被渲染,我不知道爲什麼它需要在這種情況下。我將在稍後討論這個問題 – benmarks

回答

1

男人。我需要研究這一點(我喜歡你正在採取的方法),但是如果你對基本URL進行硬編碼並且可能追加不安全的SID參數,這應該起作用。

<cms_index_index> 
    <reference name="top.links"> 
     <action method="removeLinkByUrl"><url><![CDATA[http://BASE_URL/?___SID=U]]></url></action> 
    </reference> 
</cms_index_index> 

我不喜歡這個,但它是一個開始。

+0

爲什麼使用CDATA標籤? –

+0

只是出於習慣。 – benmarks

+0

嗨只是想知道你是否在這方面取得了更多的進展,或者如果這應該被視爲最終的解決方案。謝謝! –

0

檢查,如果你在首頁上,然後做你想要什麼:

if($this->getIsHomePage()) { 
    echo 'This is Homepage'; 
} else { 
    echo 'This is NOT Homepage'; 

,或者檢查標識符名稱是網頁或不:

$routerName = Mage::app()->getRequest()->getRouteName(); 
$ident = Mage::getSingleton('cms/page')->getIdentifier(); 
if($routerName == 'cms' && $ident == 'home') { 
    echo 'This is Homepage'; 
} else { 
    echo 'This is NOT Homepage'; 
} 
+0

我的意思是,當然,但我的目標是儘可能少地寫php,並將我的定製合併到local.xml中......這樣可以在維護性路。 –

0

在page.xml將這個:

<block type="page/template_links" name="top.links" as="topLinks"> 
<action method="addLink" translate="label title" name="backhome"> 
     <label>LABEL</label> 
     <url>/</url> 
     <title>Home</title> 
     <prepare/><urlParams/><position>1</position></action>    
</block> 

這個在頁面中自定義佈局xml

... 
<reference name="top.links"> 
    <action method="removeLinkByUrl"><url>/</url></action> 
</reference> 
... 
+0

請在有關您的代碼的答案中更具描述性。請參閱:[如何回答](http://stackoverflow.com/questions/how-to-answer) – askmish