一个好主题往往需要一个友好人性的配置页面,那怎么做呢?请听我细细道来。
在主题的functions.php中新增如下语句:
require 'inc/puma-options.php'; //语句中的puma-options.php为你接下来需要新增的主题配置页面。
然后新增一个名字为puma-options.php的页面到你的主题文件夹的任意位置,具体代码如下:
<?php /** * Created by zhangsan. * User: mac * Date: 16/9/2 * Time: 上午10:54 */ function themeoptions_admin_menu() { // 在控制面板的侧边栏添加设置选项页链接 add_menu_page("主题设置", "主题选项", 'edit_themes', __FILE__, 'themeoptions_page'); } function themeoptions_page() { if ( $_POST['update_themeoptions'] == 'true' ) { themeoptions_update(); } // 这是产生主题选项页面的主要功能 ?> <div> <div id="icon-themes"><br /></div> <h2>主题设置</h2> <form method="POST" action=""> <input type="hidden" name="update_themeoptions" value="true" /> <ul> <li> <h1>1:基本设置</h1> <ul> <li> <h4>网站热词</h4> <p><textarea name="com_keywords" style="width: 60%;height: 100px;" id="com_keywords" ><?php echo get_option('puma_option_com_keywords'); ?></textarea> 请在这里输入您网站的定义热词。</p> </li> <li> <h4>网站描述</h4> <p><textarea name="com_description" style="width: 60%;height: 100px;" id="com_description" ><?php echo get_option('puma_option_com_description'); ?></textarea> 请在这里输入您网站的描述。</p> </li> </ul> </li> </ul> <p><input type="submit" name="bcn_admin_options" value="更新数据"/></p> </form> </div> <?php } function themeoptions_update() { // 数据更新验证 update_option('puma_option_com_keywords', $_POST['com_keywords']); update_option('puma_option_com_description', $_POST['com_description']); echo '更新成功!'; } add_action('admin_menu', 'themeoptions_admin_menu');
这样一个简易的主题配置页面便完成了,你只要根据实际情况在方法themeoptions_page中去添加你需要的配置项即可。
然后在页面中只要在合适的地方使用类似:
<?php echo get_option('puma_option_com_keywords'); ?>
的语句便可根据配置来进行功能的实现。