我创建了一个自定义帖子类型,需要一个WYSIWYG编辑器来显示某个人的联系信息。
显示Meta Box并显示WYSIWYG编辑器,但单击更新后内容无法保存或吐出。我究竟做错了什么?感谢。
// Create Missionary Meta Box
function my_admin() {
add_meta_box( 'missionary_meta_box',
'Missionary Contact Details',
'display_missionary_meta_box',
'missionaries', 'normal', 'high'
);
}
// Put Fields In Missionary Meta Box
function display_missionary_meta_box( $missionaries ) {
$missionary_contact_details = get_post_meta($post->ID, 'missionary_contact_details', true);
?>
<table>
<tr>
<td><?php wp_editor($missionary_contact_details, 'missionary_contact_details', array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'missionary_contact_details',
'textarea_rows' => 10,
'teeny' => true
)); ?>
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'add_missionary_fields', 10, 2 );
function add_missionary_fields( $missonaries_id, $missionaries ) {
// Check post type for missionary data
if ( $missionaries->post_type == 'missionaries' ) {
// Store data in post meta table if present in post data
if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
update_post_meta($post_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
else
delete_post_meta($post_id, 'missionary_contact_details');
}
}
答案 0 :(得分:1)
对于函数“display_missionary_meta_box”,参数名称应为$post
而不是$missionaries
,以便函数中的用户$post->ID
// Put Fields In Missionary Meta Box
function display_missionary_meta_box( $post ) {
$missionary_contact_details = get_post_meta($post->ID, 'missionary_contact_details', true);
?>
<table>
<tr>
<td><?php wp_editor($missionary_contact_details, 'missionary_contact_details', array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'missionary_contact_details',
'textarea_rows' => 10,
'teeny' => true
)); ?>
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'add_missionary_fields', 10, 1 );
function add_missionary_fields( $post_id ) {
// Check post type for missionary data
if ( $_POST['post_type'] == "missionaries") {
// Store data in post meta table if present in post data
if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
update_post_meta($post_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
else
delete_post_meta($post_id, 'missionary_contact_details');
}
}
答案 1 :(得分:0)
修改删除了我之前的语句,看起来save_post
钩子确实有一些参数,1是post_id
而另一个是帖子对象 - Reference
不一致的是,即使您将$missonaries_id
作为post_id
传递,当您保存元数据时,也会使用$post_id
这是根据此范围内不存在的变量你的代码。尝试这样的事情:
add_action( 'save_post', 'add_missionary_fields', 10, 2 );
function add_missionary_fields( $missonaries_id, $missionaries ) {
// Check post type for missionary data
if ( $missionaries->post_type == 'missionaries' ) {
// Store data in post meta table if present in post data
if(isset($_POST['missionary_contact_details']) && $_POST['missionary_contact_details'] != '')
update_post_meta($missonaries_id, 'missionary_contact_details', $_POST['missionary_contact_details']);
else
delete_post_meta($missonaries_id, 'missionary_contact_details');
}
}