上一篇教程中貼出了一個可以很方便新增後臺設定選項的類檔案,下面是該類檔案的使用方法:
一、載入類檔案
在你的主題 functions.php 檔案中使用 include_once 載入該檔案,比如上節教程的類存放於主題資料夾下的 option-class.php 檔案中:
- include_once('option-class.php');
二、檢查 js 檔案
如果你需要使用上傳圖片的選項,請根據上節教程類程式碼中 enqueue_head 函式裡面載入的 js 檔案路徑,準備好一個 js 檔案,js 程式碼在上節教程中也貼出了。
三、新建配置檔案
例如在主題資料夾中新建一耳光 option.php 檔案,也使用 include_once 載入該檔案:
- include_once('option.php');
設定選項的配置程式碼都新增在該檔案中。
四、配置選項
用編輯器開啟上面新增的 option.php,新增配置程式碼,示例程式碼如下:
注意
- 對於陣列型別、核取方塊、編輯器,配置中的 id 有要求。
- 核取方塊和陣列儲存的資料為陣列
- <?php
- $pageinfo = array('full_name' => '阿樹工作室網站設定', 'optionname'=>'ashu', 'child'=>false, 'filename' => basename(__FILE__));
- $options = array();
- $options[] = array( "type" => "open");
- $options[] = array(
- "name"=>"阿樹工作室-標題",
- "desc"=>"這是一個設定頁面示範",
- "type" => "title");
- $options[] = array(
- "name"=>"文字框",
- "id"=>"_ashu_text",
- "std"=>"阿樹工作室文字輸入框",
- "desc"=>"阿樹工作室版權所有",
- "size"=>"60",
- "type"=>"text"
- );
- $options[] = array(
- "name"=>"文字域",
- "id"=>"_ashu_textarea",
- "std"=>"阿樹工作室文字域",
- "desc"=>"阿樹工作室版權所有",
- "size"=>"60",
- "type"=>"textarea"
- );
- $options[] = array(
- "name" => "圖片上傳",
- "desc" => "請上傳一個圖片或填寫一個圖片地址",
- "std"=>"",
- "id" => "_ashu_logo",
- "type" => "upload");
- $options[] = array( "name" => "單選框",
- "desc" => "請選擇",
- "id" => "_ashu_radio",
- "type" => "radio",
- "buttons" => array('Yes','No'),
- "std" => 1);
- $options[] = array( "name" => "核取方塊",
- "desc" => "請選擇",
- "id" => "checkbox_ashu", //id 必須以 checkbox_開頭
- "std" => 1,
- "buttons" => array('汽車','腳踏車','三輪車','公交車'),
- "type" => "checkbox");
- $options[] = array( "name" => "頁面下拉框",
- "desc" => "請選擇一個頁面",
- "id" => "_ashu_page_select",
- "type" => "dropdown",
- "subtype" => 'page'
- );
- $options[] = array( "name" => "分類下拉框",
- "desc" => "請選擇大雜燴頁面",
- "id" => "_ashu_cate_select",
- "type" => "dropdown",
- "subtype" => 'cat'
- );
- $options[] = array( "name" => "分類下拉框",
- "desc" => "請選擇大雜燴頁面",
- "id" => "_ashu_side_select",
- "type" => "dropdown",
- "subtype" => 'sidebar'
- );
- $options[] = array( "name" => "下拉框",
- "desc" => "請選擇",
- "id" => "_ashu_select",
- "type" => "dropdown",
- "subtype" => array(
- '蘋果'=>'apple',
- '香蕉'=>'banana',
- '桔子'=>'orange'
- )
- );
- $options[] = array(
- "name" => "編輯器",
- "desc" => "",
- "id" => "tinymce_ashu", //id 必須以 tinymce_開頭
- "std" => "",
- "type" => "tinymce"
- );
- $options[] = array(
- "name" => "陣列資訊",
- "desc" => "請輸入一組 id,以英文分號隔開,例如 1,2,3",
- "id" => "numbers_ashu", //id 必須以 numbers_開頭
- "size"=>60,
- "std" => "",
- "type" => "numbers_array"
- );
- $options[] = array( "type" => "close");
- $options_page = new ashu_option_class($options, $pageinfo);
- ?>
最後面不要忘記類的例項化。
效果圖:

前臺呼叫 (重要):
注意,我們在類檔案中宣告瞭一個全域性變數: $ashu_option; 如果你的設定選項很多,如果前臺每次使用一個設定選項都呼叫一次 get_option 函式,這是不明智的選擇,建議使用一個全域性變數。而且我們所有的選項值都在一個選項組裡,只需要執行一次 get_option 函式即獲取了所有設定資料,所以將它儲存在一個全域性變數中。
範例:對於上面的示例設定選項,我們在首頁新增程式碼將設定資料列印出來:
- <?php
- global $ashu_option;
- var_dump($ashu_option);
- ?>
輸出結果:
- array
- 'ashu' =>
- array
- '_ashu_text' => string '阿樹工作室文字輸入框修改' (length=36)
- '_ashu_textarea' => string '阿樹工作室文字域修改' (length=30)
- '_ashu_logo' => string 'http://localhost/newtheme/wp-content/uploads/2012/08/1960_19591-360x200.jpg' (length=75)
- '_ashu_radio' => string '2' (length=1)
- '_ashu_checkbox' => string 'true' (length=4)
- '_ashu_page_select' => string '2' (length=1)
- '_ashu_cate_select' => string '1' (length=1)
- '_ashu_select' => string 'banana' (length=6)
- 'save_my_options' => string '1' (length=1)
- 'Submit' => string 'Save Changes' (length=12)
到了這裡,如何呼叫其中的資料,就不贅述了