今天成功实现wordpress主题添加主题选项的功能,看了很多代码,虽然不是很明白每句代码的意思,但是按照别人的模板
也能做出来,下面是我做的效果:

一步步学做wordpress主题–给主题添加选项-魏星博客一步步学做wordpress主题–给主题添加选项-魏星博客

这个代码是参照知更鸟的主题选项做的,主要方法代码如下:
1、建立主题选项的php页面,如:option.php,复制如下代码(部分):


cat_ID] = $category_list->cat_name;
}

$number_entries = array("Select a Number:","1","2","3","4","5","6","7","8","9","10", "12","14", "16", "18", "20" );
$options = array (

array( "name" => $themename." Options",
"type" => "title"),
//首页布局设置开始

array( "name" => "模板基本设置",
array( "type" => "close"),
"type" => "section"),
array( "type" => "open"),

array( "name" => "是否显示LOGO",
"desc" => "默认显示",
"id" => $shortname."_logo",
"type" => "select",
"std" => "Hide",
"options" => array("Display", "Hide")),

//SEO设置
array( "type" => "close"),
array( "name" => "网站SEO设置及流量统计",
"type" => "section"),
array( "type" => "open"),

array( "name" => "描述(Description)",
"desc" => "",
"id" => $shortname."_description",
"type" => "textarea",
"std" => "输入你的网站描述,一般不超过200个字符"),

array( "name" => "关键词(KeyWords)",
"desc" => "",
"id" => $shortname."_keywords",
"type" => "textarea",
"std" => "输入你的网站关键字,一般不超过100个字符"),
function mytheme_add_admin() {

global $themename, $shortname, $options;

if ( $_GET['page'] == basename(__FILE__) ) {

if ( 'save' == $_REQUEST['action'] ) {

foreach ($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }

foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ] ); } else { delete_option( $value['id'] ); } }

header("Location: admin.php?page=option.php&saved=true");
die;

}
else if( 'reset' == $_REQUEST['action'] ) {

foreach ($options as $value) {
delete_option( $value['id'] ); }

header("Location: admin.php?page=option.php&reset=true");
die;

}
}

add_theme_page($themename." Options", "当前主题设置", 'edit_themes', basename(__FILE__), 'mytheme_admin');
}

function mytheme_add_init() {

$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("functions", $file_dir."/includes/options/options.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/includes/options/rm_script.js", false, "1.0");
}
function mytheme_admin() {

global $themename, $shortname, $options;
$i=0;

if ( $_REQUEST['saved'] ) echo '

'.$themename.' 主题设置已保存

';
if ( $_REQUEST['reset'] ) echo '

'.$themename.' 主题已重新设置

';

?>

设置

当前使用主题: 仿腾讯CDC模板 | 设计者: Woallf(小星)


" />




/>





此时后台会显示上图的样子的选项管理页面,下面是调用代码,
在你要用的地方添加,如下:

文章分类


当选择分类选项为display时显示你的分类,否则不显示,上面的代码可能不是很标准,知更鸟是这样写的




没看懂的童鞋可以下载实例代码,我给打好包了,点击下面的按钮下载,自己慢慢研究吧!