編寫PHP腳本使WordPress的主題支持Widget側邊欄
更新時間:2015年12月14日 15:24:36 投稿:goldensun
這篇文章主要介紹了編寫PHP腳本使WordPress的主題支持Widget側邊欄的方法,這里以一列兩列的側邊欄為例可以以此類推更多列的寫法,需要的朋友可以參考下
幫網友小改了一下主題. 任務比較簡單, 只是為一個三欄主題添加對 Widget 的支持而已,就先從這次簡單的案例開始說吧.

單側邊欄
functions.php
<?php
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'before_widget' => '<li class="widget">', // widget 的開始標簽
'after_widget' => '</li>', // widget 的結束標簽
'before_title' => '<h3>', // 標題的開始標簽
'after_title' => '</h3>' // 標題的結束標簽
));
}
?>
sidebar.php
<div id="sidebar">
<ul class="widgets">
<?php // 如果沒有使用 Widget 才顯示以下內容, 否則會顯示 Widget 定義的內容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) :
?>
<!-- widget 1 -->
<li class="widget">
<h3>標題 1</h3>
<ul>
<li>條目 1.1</li>
<li>條目 1.2</li>
<li>條目 1.3</li>
</ul>
</li>
<!-- widget 2 -->
<li class="widget">
<h3>標題 2</h3>
<ul>
<li>條目 2.1</li>
<li>條目 2.2</li>
<li>條目 2.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
雙側邊欄
functions.php
<?php
if( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Sidebar_1', // 側邊欄 1 的名稱
'before_widget' => '<li class="widget">', // widget 的開始標簽
'after_widget' => '</li>', // widget 的結束標簽
'before_title' => '<h3>', // 標題的開始標簽
'after_title' => '</h3>' // 標題的結束標簽
));
register_sidebar(array(
'name' => 'Sidebar_2', // 側邊欄 2 的名稱
'before_widget' => '<li class="widget">', // widget 的開始標簽
'after_widget' => '</li>', // widget 的結束標簽
'before_title' => '<h3>', // 標題的開始標簽
'after_title' => '</h3>' // 標題的結束標簽
));
}
?>
sidebar.php
<div id="sidebar_1">
<ul class="widgets">
<?php // 如果沒有在側邊欄 1 中使用 Widget 才顯示以下內容, 否則會顯示 Widget 定義的內容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar_1') ) :
?>
<!-- widget 1 -->
<li class="widget">
<h3>標題 1</h3>
<ul>
<li>條目 1.1</li>
<li>條目 1.2</li>
<li>條目 1.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
<div id="sidebar_2">
<ul class="widgets">
<?php // 如果沒有在側邊欄 2 中使用 Widget 才顯示以下內容, 否則會顯示 Widget 定義的內容
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar_2') ) :
?>
<!-- widget 2 -->
<li class="widget">
<h3>標題 2</h3>
<ul>
<li>條目 2.1</li>
<li>條目 2.2</li>
<li>條目 2.3</li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
N 側邊欄
請使用數(shù)學歸納法進行推理XD
相關文章
PHP實現(xiàn)負載均衡session共享redis緩存操作示例
這篇文章主要介紹了PHP實現(xiàn)負載均衡session共享redis緩存操作,涉及php用戶登陸、session存儲、判斷等相關操作技巧,需要的朋友可以參考下2018-08-08
PHP利用MySQL保存session的實現(xiàn)思路及示例代碼
使用MySQL保存session,需要保存三個關鍵性的數(shù)據(jù):session id、session數(shù)據(jù)、session生命期,下面的示例,大家可以看看2014-09-09
How do I change MySQL timezone?
The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.2008-03-03
php連接Access數(shù)據(jù)庫錯誤及解決方法
前二天把一個asp+access的網站改成php+access的,在連連數(shù)據(jù)庫時可真讓我狠狠的郁悶了一把,通過百度了大量的相關文章終于解決了2013-06-06
通過PHP current函數(shù)獲取未知字符鍵名數(shù)組第一個元素的值
在開發(fā)中經常遇到這樣問題,獲取數(shù)組第一個元素的值,如果是數(shù)字索引那還好,直接$array[0],如果鍵名是字符串,你又未知這個字符串呢?用current()函數(shù)就可以做到2013-06-06

