自由屋推书网—热门的小说推荐平台!

你的位置: 首页 > wordpress分享

wordpress主题制作之添加后台设置

2021-10-26 22:12:06

在开发wordpress主题时,可以在后台新增一个主题配置页面,用来让使用者更方便的修改主题的一些配置,比如:更换logo,banner,添加网站关键词,统计代码等,让使用者修改主题更方便,那么接下来就学一下如何开发一个简单后台配置页面!

1.通过add_theme_page函数在配置文件functions.php中添加注册代码:

  1. //注册后台管理模块  
  2. add_action('admin_menu', 'add_theme_options_menu');  
  3.     function add_theme_options_menu() {  
  4.         add_theme_page(  
  5.             'ZYW主题设置', //页面title  
  6.             'ZYW主题设置', //后台菜单中显示的文字  
  7.             'edit_theme_options', //选项放置的位置  
  8.             'theme-options', //别名,也就是在URL中GET传送的参数  
  9.             'theme_settings_admin' //调用显示内容调用的函数  
  10.         );  
  11. }  
  12. function theme_settings_admin() {  
  13.         require get_template_directory()."/inc/zyw_settheme.php";  
  14. }  

2.主题根目录创建inc文件夹和zyw_settheme.php的文件,将以下代码复制进去

  1. <style>  
  2. .theme_set{ width:98%; }  
  3. .theme_set h2{ font-size:20px; }  
  4. .theme_set dl{ margin-top:20px; }  
  5. .theme_set dd{ margin:5px 0; }  
  6. .theme_set dd input[type=text]{ width:50%; }  
  7. .theme_set dd textarea{ width:50%; }  
  8. .theme_set dd img{ margin-bottom:-30px; }  
  9. </style>  
  10.    
  11. <?php  
  12. if($_POST['theme_set']){  
  13.    
  14. $attachment_id = media_handle_upload( 'logo', 0 ); //上传图片,返回的是 附件的ID  
  15. $logo_url = wp_get_attachment_url($attachment_id); //获取 图片的地址  
  16. if($logo_url){  
  17. update_option("logo_img",$logo_url); //如果图片地址在在,就将图片的地址写入到数据库  
  18. }  
  19.    
  20. update_option("beian",$_POST["beian"]); //更新数据表中的备案字段的值  
  21. update_option("map",$_POST["map"]);  
  22. update_option("keywords",$_POST["keywords"]);  
  23. update_option("description",$_POST["description"]);  
  24. update_option("phone_num",$_POST["phone_num"]);  
  25. update_option("qq_num",$_POST["qq_num"]);  
  26. update_option("web_static",stripslashes($_POST["web_static"]));  
  27. update_option("ad_single",stripslashes($_POST["ad_single"]));  
  28. }  
  29.    
  30. $logo_img = get_option("logo_img");  
  31. ?>  
  32. <div class="theme_set">  
  33. <form action="" method="post" enctype="multipart/form-data">  
  34. <h2>主题设置</h2>  
  35. <dl>  
  36.     <dt>网站Logo:</dt>  
  37.     <dd>  
  38.     <input type="file" name="logo"> <img src="<?php echo $logo_img; ?>" height=50 >  
  39.     </dd>  
  40. </dl>  
  41. <dl>  
  42.     <dt>网站关键词:</dt>  
  43.     <dd><input type="text" name="keywords" value="<?php echo get_option("keywords"); ?>"></dd>  
  44. </dl>  
  45. <dl>  
  46.     <dt>网站描述:</dt>  
  47.     <dd>  
  48.         <textarea name="description"><?php echo get_option("description"); ?></textarea>  
  49.     </dd>  
  50. </dl>  
  51. <dl>  
  52.     <dt>网站备案号:</dt>  
  53.     <dd><input type="text" name="beian" value="<?php echo get_option("beian"); ?>"></dd>  
  54. </dl>  
  55. <dl>  
  56.     <dt>统计代码:</dt>  
  57.     <dd>  
  58.         <textarea name="web_static" ><?php echo get_option("web_static"); ?></textarea>  
  59.     </dd>  
  60. </dl>  
  61. <dl>  
  62.     <dt>客服热线/电话:</dt>  
  63.     <dd><input type="text" name="phone_num" value="<?php echo get_option("phone_num"); ?>"> </dd>  
  64. </dl>  
  65. <dl>  
  66.     <dt>全站/客服QQ:</dt>  
  67.     <dd><input type="text" name="qq_num" value="<?php echo get_option("qq_num"); ?>"> </dd>  
  68. </dl>  
  69. <dl>  
  70.     <dt>文章页广告代码:</dt>  
  71.     <dd>  
  72.         <textarea name="ad_single" ><?php echo get_option("ad_single"); ?></textarea>  
  73.     </dd>  
  74. </dl>  
  75.    
  76. <dl>  
  77.     <dt></dt>  
  78.     <dd><input type="submit" name="theme_set" value="提交"></dd>  
  79. </dl>  
  80.    
  81. </form>  
  82. </div>  

这样,刷新后台,你会发现后台多了一个页面:外观——ZYW主题设置,这样你就可以设置主题的自定义内容了。设置之后还有一步就是调用方法:

例如:调用设置的 电话号码:

  1. <?php  
  2.             if (get_option("phone_num")) {  
  3.                 echo "<p>联系电话:".get_option("phone_num")."</p>";  
  4.             }  
  5. ?>  

编辑推荐

热门小说