如何在自定义帖子类型管理屏幕编辑页面上显示自定义数据?

时间:2016-06-04 04:10:12

标签: php wordpress woocommerce product meta-boxes

我已经创建了查询表单。当用户填写此表单时,我将其作为自定义帖子类型保存到数据库中。在后端,我需要在自定义帖子编辑页面上显示额外信息。但没有找到任何钩子。

我试过这段代码:

[![function add_extra_info_column( $columns ) {
    return array_merge( $columns, 
        array( 'sticky' => __( 'Extra Info', 'your_text_domain' ) ) );
}
add_filter( 'manage_posts_columns' , 'add_extra_info_column' );][1]][1] 

但它在自定义帖子中添加了一个新列。

当我们点击每个帖子的编辑页面链接时,我需要显示额外的信息。

1 个答案:

答案 0 :(得分:2)

这只是一个示例,您必须根据自己的需要进行自定义:

第一步:向后端添加元容器挂钩(例如此处产品发布类型):

add_action( 'add_meta_boxes', 'extra_info_add_meta_boxes' );
if ( ! function_exists( 'extra_info_add_meta_boxes' ) )
{
    function extra_info_add_meta_boxes()
    {
        global $post;

        add_meta_box( 'extra_info_data', __('Extra Info','your_text_domain'), 'extra_info_data_content', 'product', 'side', 'core' );
    }
}

(将'product'替换为您的帖子类型,这个元框可能就像'side'上的'或'正常'一样,以便在主列上显示

第二步:在此元数据框中添加信息(字段,数据,等等......)

function extra_info_data_content()
{
    global $post;
    // Here you show your data  <=====

}

参考文献:

第三步(可选)保存自定义元帖子的数据(如果您有一些字段,则需要)。

add_action( 'save_post', 'extra_info_save_woocommerce_other_fields', 10, 1 );
if ( ! function_exists( 'extra_info_save_woocommerce_other_fields' ) )
{

    function extra_info_save_woocommerce_other_fields( $post_id )
    {
        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'extra_info_other_meta_field_nonce' ] ) )
        {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'extra_info__other_meta_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) )
        {
            return $post_id;
        }

        // If this is an autosave, our form has not been submitted, so we don't want to do anything.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        {
            return $post_id;
        }

        // Check the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] )
        {
            if ( ! current_user_can( 'edit_page', $post_id ) )
            {
                return $post_id;
            }
        }
        else
        {
            if ( ! current_user_can( 'edit_post', $post_id ) )
            {
                return $post_id;
            }
        }
        /* --- !!! OK, its safe for us to save the data now. !!! --- */

        // Sanitize user input and Update the meta field in the database.
    }
}
相关问题