WordPress自定义帖子类型Meta Box tinyMCE

时间:2014-06-10 16:50:30

标签: php wordpress custom-post-type meta-boxes

我创建了一个自定义帖子类型,需要一个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');
    }
}

2 个答案:

答案 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');
    }
}