当前位置: 首页 » 标签:new_meta_boxes

wordpress给发布文章页添加自定义面板信息

在WordPress发布文章编辑器边上添加自定义面板字段,方便编辑,不用下拉寻找字段名 效果: 代码: <?php /* 后台自定义添加关键词和描述面板 时间:2014-10-24 */ //1.创建需要的字段信息 $new_meta_boxes = array( "clickurl" => array( "name" => "clickurl", "std" => "", "title" => "直达链接:"), "clickprice" => array( "name" => "clickprice", "std" => "", "title" => "优惠券金额:") , "dropdown" => array( "name" => "dropdown", "std" => "", "subtype"=>array( "tb"=>"淘宝", "tmall"=>"天猫", "jd"=>"京东" ), "title" => "来源:" ) ); //2.创建自定义字段输入框 function new_meta_boxes() { global $post, $new_meta_boxes; foreach($new_meta_boxes as $meta_box) { $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true); if($meta_box_value == "") { $meta_box_value = $meta_box['std']; } // 自定义字段标题 echo "<div style='padding:10px 0;'>".$meta_box['title']."</div>"; if(isset($meta_box['subtype'])){ echo '<select class="postform" name="'.$meta_box['name'].'_value"> '; foreach($meta_box['subtype'] as $key => $entry){ if($meta_box_value==$key){ $selected = "selected='selected'"; }else{ $selected =""; } echo '<option value="'.$key.'" '.$selected.'>'.$entry .'</option> '; } echo '</select>'; }else{ // 自定义字段输入框 echo '<input name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'"><br />'; } } echo '<input type="hidden" name="dj_metaboxes_nonce" id="dj_metaboxes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />'; } //3.创建自定义字段模块 function create_meta_box() { global $theme_name; if ( function_exists('add_meta_box') ) { add_meta_box( 'new-meta-boxes', '详情信息', 'new_meta_boxes', 'post', 'side', 'low' ); } } //4.保存数据 function save_postdata( $post_id ) { global $new_meta_boxes; if ( !wp_verify_nonce( $_POST['dj_metaboxes_nonce'], plugin_basename(__FILE__) )) return; if ( !current_user_can( 'edit_posts', $post_id )) return; foreach($new_meta_boxes as $meta_box) { $data = $_POST[$meta_box['name'].'_value']; if($data == "") delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true)); else update_post_meta($post_id, $meta_box['name'].'_value', $data); } } //5.将函数连接到指定action(动作) add_action('admin_menu', 'create_meta_box'); add_action('save_post', 'save_postdata'); ?> 添加到主题模板文件夹内的function.php里!
笔记 ·
Zero零分
站龄15年资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
476
文章
26
评论
27
分类
839
标签
7
友链
onlinelovesky 317355746 122551965@qq.com

百度广告