上一篇教程中貼出了一個可以很方便添加後台設置選項的類文件,下面是該類文件的使用方法:
一、載入類文件
在你的主題 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)
到了這裏,如何調用其中的數據,就不贅述了