[英]In Wordpress I want my sidebar to change based on the page
好的,我只是使用指南開發了我的第一個自定義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"
}
?>
如果您只想在首頁中顯示一個側邊欄,而在其余頁面中顯示其他側邊欄,則可以使用@ChrisHerbert所說的if語句。 新的解決方案是通過創建新的模板和邊欄。
sidebar.php
文件並制作一個副本。 sidebar-secondary.php
。 <?php get_sidebar('secondary'); ?>
<?php get_sidebar('secondary'); ?>
編輯
假設您想要一個側邊欄作為主頁,而將其他側邊欄分開,則可以執行以下操作。
sidebar-secondary.php
。 index.php
。 這通常將包含主頁的側邊欄。 您會找到類似<?php get_sidebar() ?>
。 page.php
。 找到get_sidebar()
並將其替換為get_sidebar('secondary')
。 現在,您的所有頁面都將使用輔助側邊欄。 如果您的任何頁面使用不同的模板,則需要執行相同的操作。 single.php
然后再次找到get_sidebar()
並將其替換為get_sidebar('secondary')
。 現在,您可以為首頁和其他頁面使用不同的側邊欄樣式和樣式。
請注意,如果您只想在首頁中使用不同的側邊欄,而在頁面其余部分中使用相同的側邊欄,則也可以在主模板文件index.php
使用有條件的。
if( is_home() ){
get_sidebar(); //your main sidebar for homepage
} else {
get_sidebar('secondary'); //your sidebar for other pages
}
絕對可以使用模板,但是假設“主頁”頁面是您的博客頁面,則您的解決方案應該可以工作。 如果將主頁設置為靜態頁面(在儀表板的“讀取”部分中),則應使用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; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.