wordpress主题后台教程-设置选项类文件使用方法

上一篇教程中贴出了一个可以很方便添加后台设置选项的类文件,下面是该类文件的使用方法:

一、载入类文件

在你的主题functions.php文件中使用include_once载入该文件,比如上节教程的类存放于主题文件夹下的option-class.php文件中:

  1. include_once('option-class.php');  

 二、检查js文件

如果你需要使用上传图片的选项,请根据上节教程类代码中 enqueue_head函数里面加载的js文件路径,准备好一个js文件,js代码在上节教程中也贴出了。

三、新建配置文件

例如在主题文件夹中新建一耳光option.php文件,也使用include_once载入该文件:

  1. include_once('option.php');   

设置选项的配置代码都添加在该文件中。

四、配置选项

用编辑器打开上面添加的option.php,添加配置代码,示例代码如下:

注意

  1. 对于数组类型、复选框、编辑器,配置中的id有要求。
  2. 复选框和数组保存的数据为数组
  1. <?php      
  2.      
  3. $pageinfo = array('full_name' => '阿树工作室网站设置', 'optionname'=>'ashu', 'child'=>false, 'filename' => basename(__FILE__));      
  4.      
  5. $options = array();      
  6.                   
  7. $options[] = array"type" => "open");      
  8.      
  9. $options[] = array(       
  10.                 "name"=>"阿树工作室-标题",      
  11.                 "desc"=>"这是一个设置页面示范",      
  12.                 "type" => "title");      
  13.                       
  14. $options[] = array(      
  15.                 "name"=>"文本框",      
  16.                 "id"=>"_ashu_text",      
  17.                 "std"=>"阿树工作室文本输入框",      
  18.                 "desc"=>"阿树工作室版权所有",      
  19.                 "size"=>"60",      
  20.                 "type"=>"text"     
  21.             );      
  22.      
  23. $options[] = array(      
  24.                 "name"=>"文本域",      
  25.                 "id"=>"_ashu_textarea",      
  26.                 "std"=>"阿树工作室文本域",      
  27.                 "desc"=>"阿树工作室版权所有",      
  28.                 "size"=>"60",      
  29.                 "type"=>"textarea"     
  30.             );      
  31.      
  32. $options[] = array(      
  33.             "name" => "图片上传",      
  34.             "desc" => "请上传一个图片或填写一个图片地址",      
  35.             "std"=>"",      
  36.             "id" => "_ashu_logo",      
  37.             "type" => "upload");              
  38.      
  39. $options[] = array(  "name" => "单选框",      
  40.             "desc" => "请选择",      
  41.             "id" => "_ashu_radio",      
  42.             "type" => "radio",      
  43.             "buttons" => array('Yes','No'),      
  44.             "std" => 1);      
  45.      
  46. $options[] = array"name" => "复选框",      
  47.             "desc" => "请选择",      
  48.             "id" => "checkbox_ashu",   //id必须以checkbox_开头   
  49.             "std" => 1,     
  50.             "buttons" => array('汽车','自行车','三轮车','公交车'),                
  51.             "type" => "checkbox");      
  52.                   
  53. $options[] = array"name" => "页面下拉框",      
  54.             "desc" => "请选择一个页面",      
  55.             "id" => "_ashu_page_select",      
  56.             "type" => "dropdown",      
  57.             "subtype" => 'page'      
  58.             );      
  59.      
  60. $options[] = array"name" => "分类下拉框",      
  61.             "desc" => "请选择大杂烩页面",      
  62.             "id" => "_ashu_cate_select",      
  63.             "type" => "dropdown",      
  64.             "subtype" => 'cat'      
  65.             );   
  66.   
  67. $options[] = array"name" => "分类下拉框",      
  68.             "desc" => "请选择大杂烩页面",      
  69.             "id" => "_ashu_side_select",      
  70.             "type" => "dropdown",      
  71.             "subtype" => 'sidebar'      
  72.             );      
  73.                   
  74. $options[] = array"name" => "下拉框",      
  75.             "desc" => "请选择",      
  76.             "id" => "_ashu_select",      
  77.             "type" => "dropdown",      
  78.             "subtype" => array(      
  79.                 '苹果'=>'apple',      
  80.                 '香蕉'=>'banana',      
  81.                 '桔子'=>'orange'      
  82.                 )      
  83.             );   
  84.                
  85. $options[] = array(      
  86.             "name" => "编辑器",      
  87.             "desc" => "",      
  88.             "id" => "tinymce_ashu",   //id必须以 tinymce_开头   
  89.             "std" => "",   
  90.             "type" => "tinymce"     
  91.             );    
  92. $options[] = array(      
  93.             "name" => "数组信息",      
  94.             "desc" => "请输入一组id,以英文分号隔开,例如 1,2,3",      
  95.             "id" => "numbers_ashu"//id必须以 numbers_开头   
  96.             "size"=>60,   
  97.             "std" => "",   
  98.             "type" => "numbers_array"     
  99.             );             
  100.                   
  101. $options[] = array"type" => "close");      
  102.      
  103. $options_page = new ashu_option_class($options$pageinfo);      
  104. ?>  

最后面不要忘记类的实例化。

效果图:使用类文件配置设置选项

 

前台调用(重要):

注意,我们在类文件中声明了一个全局变量: $ashu_option;  如果你的设置选项很多,如果前台每次使用一个设置选项都调用一次get_option函数,这是不明智的选择,建议使用一个全局变量。而且我们所有的选项值都在一个选项组里,只需要执行一次get_option函数即获取了所有设置数据,所以将它保存在一个全局变量中。

范例:对于上面的示例设置选项,我们在首页添加代码将设置数据打印出来:

  1. <?php   
  2.     global $ashu_option;   
  3.     var_dump($ashu_option);   
  4. ?>  

输出结果:

  1. array   
  2.   'ashu' =>    
  3.     array   
  4.       '_ashu_text' => string '阿树工作室文本输入框修改' (length=36)   
  5.       '_ashu_textarea' => string '阿树工作室文本域修改' (length=30)   
  6.       '_ashu_logo' => string 'http://localhost/newtheme/wp-content/uploads/2012/08/1960_19591-360x200.jpg' (length=75)   
  7.       '_ashu_radio' => string '2' (length=1)   
  8.       '_ashu_checkbox' => string 'true' (length=4)   
  9.       '_ashu_page_select' => string '2' (length=1)   
  10.       '_ashu_cate_select' => string '1' (length=1)   
  11.       '_ashu_select' => string 'banana' (length=6)   
  12.       'save_my_options' => string '1' (length=1)   
  13.       'Submit' => string 'Save Changes' (length=12)  

到了这里,如何调用其中的数据,就不赘述了

本篇教程之前的几篇教程是

本篇教程之后的几篇教程是

没有找到你要找的内容?你可以通过搜索你要找的内容,或者给我们留言。

已有60条评论

阿树工作室进行回复 取消回复