当前位置:秋硕笔记 > 记录 > 正文

来自小影博客的轻量级主题后台框架

写在前面
最近在学习主题后台框架。今天度娘发现了一款小影主题的轻量级后台框架,感觉非常简单而且使用效果不错。参考了橘子书博客乐趣公园博客的博文,记录如下,推荐给其他需要的朋友。
后台预览:来自小影博客的轻量级主题后台框架

基础代码
框架是用一个数组构建一个选项的
[cc]
array(
‘name’ => ‘单选项设置’,
‘desc’ => ‘选择一个参数作为排序的根据,可以给与几个选择并且选择一个,可以预留选项’,
‘id’ => ‘git_hot_b’,
‘type’ => ‘radio’,
‘options’ => array(
‘选择一’ => ‘xuanze1’,
‘选择二’ => ‘xuanze2’,
‘选择三’ => ‘xuanze3’,
‘选择四’ => ‘xuanze4’,
‘选择五’ => ‘xuanze5’,
‘选择六’ => ‘xuanze6’,
‘选择七’ => ‘xuanze7’,
‘选择八’ => ‘xuanze8’
),
‘std’ => ‘xuanze1’
),
[/cc]
主题的数据调用也比较简单,上面单选的调用方式如下
[cc]

[/cc]

使用方法
1.引入主题框架文件:将theme-options.php文件扔到主题根目录,然后打开functions,写入以下代码:
[cc]require get_template_directory() . ‘/theme-options.php’;[/cc]
2.框架可使用的ID类型
[cc]
type类型:
subtitle //二级标题
number //数字框
checkbox //复选框
select //单选框
password //密码框(密码不可见)
radio //单选框
text //文本框
textarea //多行文本框
[/cc]
3.调用函数
[cc]
//调用函数
get_option(‘ID’)

//条件判断
if (get_option(‘ID’) == true){
内容
}

//调用输出

[/cc]
4.框架数组构建选项
前端调用方式:
[cc]

[/cc]
完整代码
@橘子书博客的theme-options.php文件的完整代码:
[cc]
‘id’ => ‘panel_general’,//标签的ID
‘type’ => ‘panelstart’ //顶部标签的类型
),
array(
‘name’ => ‘数字选择框’,
‘desc’ => ‘这个是数字输入框’,
‘id’ => ‘git_linkpage_cat’,
‘type’ => ‘number’,
‘std’ => ‘2’//最后一个数组不需要逗号
),
array(
‘name’ => ‘选择选项’,
‘desc’ => ‘勾选选项的描述文字’,
‘id’ => ‘git_thumbnail_b’,
‘type’ => ‘checkbox’//复选框
),
array(
‘name’ => ‘单选项设置’,
‘desc’ => ‘选择一个参数作为排序的根据,可以给与几个选择并且选择一个,可以预留选项’,
‘id’ => ‘git_hot_b’,
‘type’ => ‘radio’,
‘options’ => array(
‘选择一’ => ‘xuanze1’,
‘选择二’ => ‘xuanze2’,
‘选择三’ => ‘xuanze3’,
‘选择四’ => ‘xuanze4’,
‘选择五’ => ‘xuanze5’,
‘选择六’ => ‘xuanze6’,
‘选择七’ => ‘xuanze7’,
‘选择八’ => ‘xuanze8’
),
‘std’ => ‘xuanze1’
),
array(
‘name’ => ‘复选项设置’,
‘desc’ => ”,
‘id’ => ‘git_hot_b4’,
‘type’ => ‘checkboxs’,
‘options’ => array(
‘xuanze14’ => ‘选择一’,
‘xuanze24’ => ‘选择二’,
‘xuanze34’ => ‘选择三’,
‘xuanze44’ => ‘选择四’,
‘xuanze54’ => ‘选择五’
),
‘std’ => ‘xuanze14’
),
array(
‘name’ => ‘单选项设置’,
‘desc’ => ‘选择一个参数作为排序的根据,可以给与几个选择并且选择一个,可以预留选项’,
‘id’ => ‘git_hot_b5’,
‘type’ => ‘select’,
‘options’ => array(
‘xuanze12’ => ‘选择一’,
‘xuanze22’ => ‘选择二’,
‘xuanze32’ => ‘选择三’,
‘xuanze42’ => ‘选择四’,
‘xuanze52’ => ‘选择五’
),
‘std’ => ‘xuanze52’
),
array(
‘name’ => ‘密码选项输入框’,
‘desc’ => ‘这是一个密码输入框,所以不可见’,
‘id’ => ‘git_wbpasd_b’,
‘type’ => ‘password’,
‘std’ => ”
),
array(
‘title’ => ‘这是一个分段,也是一个二级标题’,//二级标题,只显示文字,没有选项
‘type’ => ‘subtitle’//二级标题的类型
),
array(
‘name’ => ‘文字选项’,
‘desc’ => ‘这里是输入框的描述文字’,
‘id’ => ‘hot_list_title’,
‘type’ => ‘text’,
‘std’ => ‘主题预留文字’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘这里是输入框的描述文字’,//这里可以随便写的
‘id’ => ‘’git_tui’,//ID是保存数据的值,保持唯一性
‘type’ => ‘textarea’,//设置选项的类型
‘std’ => ‘这里是选项的默认数据’//选项的默认数据
),
array(
‘name’ => ‘文字选项设置’,
‘desc’ => ‘选项的描述文字’,
‘id’ => ‘git_tougao_mailto’,
‘type’ => ‘text’,
‘std’ => get_bloginfo( ‘admin_email’ ) //亮点是默认值里面可以用函数调用
),
array(
‘type’ => ‘panelend’//标签段的结束
),
array(
‘title’ => ‘选项二’,
‘id’ => ‘panel_seo’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo1’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’//标签段的结束
),
array(
‘title’ => ‘选项三’,
‘id’ => ‘panel_aritical’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo2’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项四’,
‘id’ => ‘panel_stylish’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo3’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项五’,
‘id’ => ‘panel_slide’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo4’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项六’,
‘id’ => ‘panel_cat’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo5’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项七’,
‘id’ => ‘panel_social’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo6’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项八’,
‘id’ => ‘panel_footer’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo7’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项九’,
‘id’ => ‘panel_ads’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo8’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项十’,
‘id’ => ‘panel_plugin’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo9’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项十一’,
‘id’ => ‘panel_advence’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo10’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
)
);
//主题后台设置已完成,下面可以不用看了
function git_add_theme_options_page() {
global $options;
if ($_GET[‘page’] == basename(__FILE__)) {
if (‘update’ == $_REQUEST[‘action’]) {
foreach($options as $value) {
if (isset($_REQUEST[$value[‘id’]])) {
update_option($value[‘id’], $_REQUEST[$value[‘id’]]);
} else {
delete_option($value[‘id’]);
}
}
update_option(‘git_options_setup’, true);
header(‘Location: themes.php?page=theme-options.php&update=true’);
die;
} else if( ‘reset’ == $_REQUEST[‘action’] ) {
foreach ($options as $value) {
delete_option($value[‘id’]);
}
delete_option(‘git_options_setup’);
header(‘Location: themes.php?page=theme-options.php&reset=true’);
die;
}
}
add_theme_page(‘主题选项’, ‘主题选项’, ‘edit_theme_options’, basename(__FILE__) , ‘git_options_page’);
}
add_action(‘admin_menu’, ‘git_add_theme_options_page’);

function git_options_page() {
global $options;
$optionsSetup = get_option(‘git_options_setup’) != ”;
if ($_REQUEST[‘update’]) echo ‘

设置已保存。

‘;
if ($_REQUEST[‘reset’]) echo ‘

设置已重置。

‘;
?>

主题选项

?>


$panelIndex = 0;
foreach ($options as $value) {
switch ( $value[‘type’] ) {
case ‘panelstart’://最高标签
echo ‘

‘;$panelIndex++;break;case ‘panelend’:echo ‘

‘;
break;
case ‘subtitle’:
echo ‘

‘ . $value[‘title’] . ‘

‘;
break;
case ‘text’:
?>

 

 

[]” checked=”checked” name=”[]” type=”checkbox” value=”“>>




其他事项说明

这里可以加入主题的使用文档





}
[/cc]
@乐趣公园博客的完整代码:
[cc]
‘id’ => ‘panel_general’,//标签的ID
‘type’ => ‘panelstart’ //顶部标签的类型
),
array(
‘name’ => ‘数字选择框’,
‘desc’ => ‘这个是数字输入框’,
‘id’ => ‘git_linkpage_cat’,
‘type’ => ‘number’,
‘std’ => ‘2’//最后一个数组不需要逗号
),
array(
‘name’ => ‘选择选项’,
‘desc’ => ‘勾选选项的描述文字’,
‘id’ => ‘git_thumbnail_b’,
‘type’ => ‘checkbox’//复选框
),
array(
‘name’ => ‘单选项设置’,
‘desc’ => ‘选择一个参数作为排序的根据,可以给与几个选择并且选择一个,可以预留选项’,
‘id’ => ‘git_hot_b’,
‘type’ => ‘radio’,
‘options’ => array(
‘选择一’ => ‘xuanze1’,
‘选择二’ => ‘xuanze2’,
‘选择三’ => ‘xuanze3’,
‘选择四’ => ‘xuanze4’,
‘选择五’ => ‘xuanze5’,
‘选择六’ => ‘xuanze6’,
‘选择七’ => ‘xuanze7’,
‘选择八’ => ‘xuanze8’
),
‘std’ => ‘xuanze1’
),
array(
‘name’ => ‘复选项设置’,
‘desc’ => ”,
‘id’ => ‘git_hot_b4’,
‘type’ => ‘checkboxs’,
‘options’ => array(
‘xuanze14’ => ‘选择一’,
‘xuanze24’ => ‘选择二’,
‘xuanze34’ => ‘选择三’,
‘xuanze44’ => ‘选择四’,
‘xuanze54’ => ‘选择五’
),
‘std’ => ‘xuanze14’
),
array(
‘name’ => ‘单选项设置’,
‘desc’ => ‘选择一个参数作为排序的根据,可以给与几个选择并且选择一个,可以预留选项’,
‘id’ => ‘git_hot_b5’,
‘type’ => ‘select’,
‘options’ => array(
‘xuanze12’ => ‘选择一’,
‘xuanze22’ => ‘选择二’,
‘xuanze32’ => ‘选择三’,
‘xuanze42’ => ‘选择四’,
‘xuanze52’ => ‘选择五’
),
‘std’ => ‘xuanze52’
),
array(
‘name’ => ‘密码选项输入框’,
‘desc’ => ‘这是一个密码输入框,所以不可见’,
‘id’ => ‘git_wbpasd_b’,
‘type’ => ‘password’,
‘std’ => ”
),
array(
‘title’ => ‘这是一个分段,也是一个二级标题’,//二级标题,只显示文字,没有选项
‘type’ => ‘subtitle’//二级标题的类型
),
array(
‘name’ => ‘文字选项’,
‘desc’ => ‘这里是输入框的描述文字’,
‘id’ => ‘hot_list_title’,
‘type’ => ‘text’,
‘std’ => ‘主题预留文字’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘这里是输入框的描述文字’,//这里可以随便写的
‘id’ => ‘’git_tui’,//ID是保存数据的值,保持唯一性
‘type’ => ‘textarea’,//设置选项的类型
‘std’ => ‘这里是选项的默认数据’//选项的默认数据
),
array(
‘name’ => ‘文字选项设置’,
‘desc’ => ‘选项的描述文字’,
‘id’ => ‘git_tougao_mailto’,
‘type’ => ‘text’,
‘std’ => get_bloginfo( ‘admin_email’ ) //亮点是默认值里面可以用函数调用
),
array(
‘type’ => ‘panelend’//标签段的结束
),
array(
‘title’ => ‘选项二’,
‘id’ => ‘panel_seo’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo1’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’//标签段的结束
),
array(
‘title’ => ‘选项三’,
‘id’ => ‘panel_aritical’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo2’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项四’,
‘id’ => ‘panel_stylish’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo3’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项五’,
‘id’ => ‘panel_slide’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo4’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项六’,
‘id’ => ‘panel_cat’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo5’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项七’,
‘id’ => ‘panel_social’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo6’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项八’,
‘id’ => ‘panel_footer’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo7’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项九’,
‘id’ => ‘panel_ads’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo8’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项十’,
‘id’ => ‘panel_plugin’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo9’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
),
array(
‘title’ => ‘选项十一’,
‘id’ => ‘panel_advence’,
‘type’ => ‘panelstart’
),
array(
‘name’ => ‘文本框选项’,
‘desc’ => ‘描述文字’,
‘id’ => ‘git_demo10’,
‘type’ => ‘textarea’,
‘std’ => ”
),
array(
‘type’ => ‘panelend’
)
);
//主题后台设置已完成,下面可以不用看了
function git_add_theme_options_page() {
global $options;
if ($_GET[‘page’] == basename(__FILE__)) {
if (‘update’ == $_REQUEST[‘action’]) {
foreach($options as $value) {
if (isset($_REQUEST[$value[‘id’]])) {
update_option($value[‘id’], $_REQUEST[$value[‘id’]]);
} else {
delete_option($value[‘id’]);
}
}
update_option(‘git_options_setup’, true);
header(‘Location: themes.php?page=theme-options.php&update=true’);
die;
} else if( ‘reset’ == $_REQUEST[‘action’] ) {
foreach ($options as $value) {
delete_option($value[‘id’]);
}
delete_option(‘git_options_setup’);
header(‘Location: themes.php?page=theme-options.php&reset=true’);
die;
}
}
add_theme_page(‘主题选项’, ‘主题选项’, ‘edit_theme_options’, basename(__FILE__) , ‘git_options_page’);
}
add_action(‘admin_menu’, ‘git_add_theme_options_page’);

function git_options_page() {
global $options;
$optionsSetup = get_option(‘git_options_setup’) != ”;
if ($_REQUEST[‘update’]) echo ‘

设置已保存。

‘;
if ($_REQUEST[‘reset’]) echo ‘

设置已重置。

‘;
?>

主题选项

?>


$panelIndex = 0;
foreach ($options as $value) {
switch ( $value[‘type’] ) {
case ‘panelstart’://最高标签
echo ‘

‘;$panelIndex++;break;case ‘panelend’:echo ‘

‘;
break;
case ‘subtitle’:
echo ‘

‘ . $value[‘title’] . ‘

‘;
break;
case ‘text’:
?>

 

 

[]” checked=”checked” name=”[]” type=”checkbox” value=”“>>




其他事项说明

这里可以加入主题的使用文档





}
[/cc]

使用方式:在函数文件里调用。
[cc]
require get_template_directory() . ‘/theme-options.php’;
[/cc]

文件下载
云落博客提供的框架下载:点击下载
龙砚庭博客提供的框架下载:https://pan.baidu.com/s/1kVOIaR9,提取码:5rvq
云落开发的git主题:点击下载

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏

评论抢沙发