Mon*_*ski 5 php wordpress custom-post-type
请帮助我..我是 WordPress 新手,我使用字段(位置、着装)的 custom_meta_box 创建自定义帖子
因此,在我的自定义帖子列表中,我想查看我在 custom_meta_box 上创建的值。
这是我当前的代码:
/*
插件名称:我自己的自定义帖子
插件 URI:http://www.mywebsite.com/firstPlugin/
描述:我的样本描述
作者:蒙斯基
版本:1.0
作者 URI:http://www.mywebsite.com/
*/
// 注册新的帖子类型和分类
函数 wpt_event_posttype() {
register_post_type( '事件',
大批(
'标签' => 数组(
'名称' => __( '活动' ),
'singular_name' => __( '事件' ),
'add_new' => __( '添加新事件' ),
'add_new_item' => __( '添加新事件' ),
'edit_item' => __( '编辑事件' ),
'new_item' => __( '添加新事件' ),
'view_item' => __( '查看事件' ),
'search_items' => __('搜索事件'),
'not_found' => __( '未找到事件' ),
'not_found_in_trash' => __( '垃圾箱中未发现事件' )
),
'公共' => 真实,
'支持' => array( '标题', '编辑器', '缩略图', '评论' ),
'capability_type' => '帖子',
'rewrite' => array("slug" => "events"), // 永久链接格式
'菜单位置' => 5,
'register_meta_box_cb' => 'add_events_metaboxes'
)
);
}
add_action( 'init', 'wpt_event_posttype' );
函数 add_events_metaboxes() {
// add_meta_box('wpt_events_date', '活动日期', 'wpt_events_date', '事件', 'side', '默认');
add_meta_box('wpt_events_location', '活动地点', 'wpt_events_location', '事件', '正常', '高');
}
add_action( 'add_meta_boxes', 'add_events_metaboxes' );
函数 wpt_events_location() {
全球$post;
// 需要 Noncename 来验证数据的来源
回声'';
// 如果已经输入,则获取位置数据
$location = get_post_meta($post->ID, '_location', true);
$dresscode = get_post_meta($post->ID, '_dresscode', true);
// 回显字段
回声'输入位置:
';
回声'';
回声'人们应该如何着装?
';
回声'';
}
函数 wpt_save_events_meta($post_id, $post) {
// 验证它来自我们的屏幕并经过适当的授权,
// 因为save_post可以在其他时间触发
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
返回$post->ID;
}
// 是否允许用户编辑帖子或页面?
if ( !current_user_can( 'edit_post', $post->ID ))
返回$post->ID;
// 好的,我们已经通过身份验证:我们需要查找并保存数据
// 我们将把它放入一个数组中以使其更容易循环。
$events_meta['_location'] = $_POST['_location'];
$events_meta['_dresscode'] = $_POST['_dresscode'];
// 添加 $events_meta 的值作为自定义字段
foreach ($events_meta as $key => $value) { // 循环遍历 $events_meta 数组!
if( $post->post_type == '修订版' ) return; // 不要重复存储自定义数据
$值 = 内爆(',', (数组)$值); // 如果 $value 是一个数组,则将其设为 CSV(不太可能)
if(get_post_meta($post->ID, $key, FALSE)) { // 如果自定义字段已经有值
update_post_meta($post->ID, $key, $value);
} else { // 如果自定义字段没有值
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // 如果为空则删除
}
}
add_action('save_post', 'wpt_save_events_meta', 1, 2); // 保存自定义字段