【架構篇】WordPress 佈景主題製作【functions.php】

【架構篇】Wordpress 佈景主題製作【functions.php】

本篇要來介紹製作WordPress佈景主題之一的functions.php檔案,functions.php是用來專門放置函式檔,主要是用來載入像是留言板、側邊欄、短碼、選單…等等工具,有點像是將一堆工具放到這檔案,之後就能在其他檔案中快速取用。

使用方法很簡單,只需要在functions.php加入語法即可。

像是 sidebar.php 中使用的側邊欄,放到 functions.php 函式檔後就能在其他頁面取用,如果像是要建立多組的側邊欄,也只需要在 functions.php 上繼續加上。

範例:

<?php
function theme_slug_widgets_init() {
if ( function_exists('register_sidebar') ):
register_sidebar(array(
'name' => '側邊欄',
'id' => 'sidebar',
'description' => '顯示於每個網頁的右方。',
'before_widget' => '<section id="%1$s" class="sidebar-right">',
'after_widget' => '</section>',
'before_title' => '<div class="sidebar-title"><h1>',
'after_title' => '</h1></div>'
));
add_action( 'widgets_init', 'theme_slug_widgets_init' );
?>

注意事項:
建議不要在 functions.php 內留空白列,不然有可能會讓 WordPress 發生錯誤。

關於WordPress系列文章事項
文章版本:
第 2.0 版本,WordPress 系列教學純屬個人經驗,若有錯誤會重新修正並加上版本號碼,目前會持續調整樣式、整理佈景語法,目前版本已調整為先教學基本架構再教學設計樣式,之後會以最終版本告知。

相關文章
作者簡介
個人頭像照片
努力寫文中!