13071077710
您的当前位置: 首页 > wordpress > WordPress添加自定义字段栏目面板

WordPress添加自定义字段栏目面板

时间: 2016-5-22

一·在functions.php 添加一下代码

/*
*WordPress添加自定义字段栏目面板
*http://www.ludou.org/creating-custom-write-panels-in-wordpress.html
*/
$new_meta_boxes = /*创建需要的字段信息:*/
array(
 "tupian_one" => array(
 "name" => "_tupian_one",
 "std" => "",
 "title" => "图片链接:"),

 "tupian_alt" => array(
 "name" => "_tupian_alt",
 "std" => "",
 "title" => "名称:")
);
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'<h4>'.$meta_box['title'].'</h4>';
 
 // 自定义字段输入框
 echo '<textarea cols="60" rows="1" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />';
 }
 
 echo '<input type="hidden" name="newmetaboxes_noncename" id="newmetaboxes_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
function create_meta_box() { /*创建自定义字段模块*/
 global $theme_name;
 
 if ( function_exists('add_meta_box') ) {
 add_meta_box( 'new-meta-boxes', '自定义模块', 'new_meta_boxes', 'post', 'normal', 'high' );
 }
}
function save_postdata( $post_id ) {/*保存文章数据*/
 global $new_meta_boxes;
 
 if ( !wp_verify_nonce( $_POST['newmetaboxes_noncename'], 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(get_post_meta($post_id, $meta_box['name'].'_value') == "")
 add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
 elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
 update_post_meta($post_id, $meta_box['name'].'_value', $data);
 elseif($data == "")
 delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
 }
}
add_action('admin_menu', 'create_meta_box');/*将函数连接到指定action(动作)*/
add_action('save_post', 'save_postdata');//End

二·调用

<?php $tupian_one = get_post_custom_values("_tupian_one_value"); echo $tupian_one[0];?>

三·使用案例 查看本站的网站分类

Copyright © 2013-2019 子歇    版权所有   网站地图