2013-08-02 25 views
1

好,我只是使用引導開發了我的第一個自定義WordPress的主題 - 我還是個菜鳥。我沒有使用小工具填充側邊欄,我想用自己的代碼和圖像自己填寫。在WordPress我希望我的側邊欄來改變基於頁面

因此,可以說我想我做了只在主頁上顯示了第一個欄,然後我做了第二個欄顯示了所有其他網頁上。我可以使用if語句嗎?

<?php 

if(is_home()){ 

echo 

"<div class="row"> 
      <div class="span3"> 
       <div id="search-box"><img id="search_text" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/text_findyourhome.png"> 
       </div> 
      </div> 
     </div> 


     <div class="row"> 
      <div class="span3"> 
       <img class="box_shadow" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/box-shadow.png" style="padding-left:0;"> 
      </div> 
     </div> 

     <div class="row"> 
      <div class="span3"><div id="news" style="margin-top:275px;margin-bottom:200px">News</div></div> 
     </div>" 
     } 

     else 
     { 
     echo 
     "sidebar 2 code - didn't write it yet" 
     }  
?> 
+1

我會建議在PHP中使用模板和頁面而不是if語句。 http://codex.wordpress.org/Templates http://codex.wordpress.org/Pages – zajd

回答

1

可以if語句由@ChrisHerbert說,如果你只是想顯示一個側邊欄使用主頁和其他頁面的其他內容。新的解決方案將通過創建一個新的模板和邊欄。

  1. 首先找到你的sidebar.php文件並複製一份。
  2. 名稱使用wordpress naming convention例如側邊欄:sidebar-secondary.php
  3. 根據需要在sidebar-secondary.php中進行更改。
  4. 創建一個新的template和使用您的邊欄以<?php get_sidebar(‘secondary’); ?>

編輯

假設你想爲主頁和獨立於其他所有你可以做下面的一個側邊欄。

  1. 創建sidebar-secondary.php如上所述。主要模板爲index.php。這通常會包含您主頁的側邊欄。你會發現一些代碼,如<?php get_sidebar() ?>
  2. 如果你想比如說網頁側邊欄中學,打開你的頁面模板page.php。找到get_sidebar()行並將其替換爲get_sidebar('secondary')。現在,您的所有網頁都將使用輔助側邊欄。如果您的任何頁面使用不同的模板,則需要執行相同的操作。
  3. 要在您的單個帖子頁面中顯示次要側邊欄(用戶在博客部分中單擊閱讀時顯示的頁面),請打開single.php,然後再次找到並用get_sidebar('secondary')替換get_sidebar()

現在,您可以風格和不同的方式使用你的側邊欄的主頁和其他頁面。

請注意,如果你只是想在網頁側邊欄的不同和相同的頁面的其餘部分,您還可以使用條件在主模板文件index.php

if(is_home()){ 
get_sidebar(); //your main sidebar for homepage 
} else { 
get_sidebar('secondary'); //your sidebar for other pages 
} 
+0

我有點困惑。如果我製作模板,我是否還需要使用if語句?如果我有常規的sidebar.php和sidebar-secondary.php,我不需要根據它在index.php文件中的哪個頁面來調用每一個?我瞭解你告訴我製作兩個單獨的側邊欄php文件的部分。我不明白在哪裏編排他們出現在不同的頁面上。請告訴我。 – Mike

+0

或者我會將它從index.php文件中刪除,並且只需在每個頁面的內容區域中調用sidebar.php或sidebar-secondary.php - 是您要說的內容? – Mike

+0

沒關係我無法在內容區域 – Mike

0

模板絕對是要走的路,但是您的解決方案應該假設您的「主頁」頁面是您的博客頁面。如果您已將主頁設置爲靜態頁面(位於儀表板的「閱讀」部分中),則應使用is_front_page()功能而不是is_home()

我也建議使用替代語法和您的標記之前關閉PHP標籤,就像這樣:

<?php if (is_home()) : ?> 

    <div class="row"> 
     <div class="span3"> 
      <div id="search-box"><img id="search_text" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/text_findyourhome.png"> 
      </div> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="span3"> 
      <img class="box_shadow" src="http://localhost/Dorsa/wp-content/themes/Bootstrap/img/box-shadow.png" style="padding-left:0;"> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="span3"><div id="news" style="margin-top:275px;margin-bottom:200px">News</div></div> 
    </div> 

<?php else: ?> 

    <!-- sidebar 2 code - didn't write it yet" --> 

<?php endif; ?> 
相關問題