[WP]修改theme增加Sidebar
八月 4, 2008
以 WP 預設就有的 default 佈景主題(如上圖)為例,要如何在頁尾(footer)的地方可以動態的加上模組(widget) 呢?
- 修改 functions.php 註冊新的 sidebar
- 修改 footer.php 決定 sidebar 的位置
- 從管理介面增加模組
修改 functions.php
在 \wp-content\themes\default\functions.php 一開始就就註冊(register_sidebar#1)了原本右邊的sidebar。
只要在下面接著加入這段程式就可以在管理介面中看到新的 sidebar 了。
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar_bottom',
'before_widget' => '<li id="%1$s" class="widget %2$s" x>',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
修改 footer.php
接下來就要決定 Sidebar_bottom 要放那兒了。
因為我們要放在版面下方,所以要修改 footer.php 。
在 \wp-content\themes\default\footer.php 檔案一開始加入程式。
<br clear="all" />
<div id="Sidebar_bottom">
<?php if ( !function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Sidebar_bottom') ) : ?>
<p>可以從「Design \ Widgets」設定此內容</p>
<?php endif; ?>
</div>
如果使用者沒有在 Sidebar_bottom 加入模組就會顯示中間的文字。
從管理介面增加模組
進入管理介面後就可以隨意的加入模組了。
說明:
- register_sidebar 定義在 wp-includes/widget.php