将自定义字段添加到自定义帖子类型

时间:2015-06-27 作者:raju

我想添加文件上载控件的自定义字段,以便在我的自定义帖子类型页面中上载图像文件(新建/编辑)。我在谷歌上搜索过,但没有找到教程。请帮帮我。提前谢谢。

1 个回复
SO网友:Karun

要添加元框,必须使用add_meta_box 允许我们向管理界面添加元框的函数。但仅仅添加一个元框并不能使其工作。你还必须有一个callback function 为编辑屏幕部分打印HTML。

在您的情况下,我们需要回调函数来显示用户可以控制上载图像的区域。

为此,我们需要加载如下脚本media-uploadthickbox. 以下示例将帮助您实现需求。

这里有一些评论可以帮助您正确理解。

    function ks_admin_styles() {
         wp_enqueue_style(\'thickbox\');
    }
    add_action(\'admin_print_styles\', \'ks_admin_styles\');        

    function ks_custom_fields(){
        /* Fire our meta box setup function on the post editor screen. */
        add_action( \'load-post.php\', \'post_meta_boxes_setup\' );
        add_action( \'load-post-new.php\', \'post_meta_boxes_setup\' );
    }
    add_action(\'init\',\'ks_custom_fields\');

    /* Meta box setup function. */
    function post_meta_boxes_setup() {
        /* Add meta boxes on the \'add_meta_boxes\' hook. */
        add_action( \'add_meta_boxes\', \'add_post_meta_boxes\' );

        /* Save post meta on the \'save_post\' hook. */
        add_action( \'save_post\', \'save_post_class_meta\', 10, 2 );
    }

    /* Create one or more meta boxes to be displayed on the post editor screen. */
    function add_post_meta_boxes() {
        add_meta_box(
            \'ks-post-class\',      // Unique ID
            esc_html__( \'Post Class\', \'example\' ),    // Title
            \'post_class_meta_box\',   // Callback function
            \'portfolio\',         // Admin page (or post type)
            \'side\',         // Context
            \'default\'         // Priority
        );
    }

    /* Display the post meta box. */
    function post_class_meta_box( $object, $box ) { ?>
      <?php wp_nonce_field( basename( __FILE__ ), \'post_class_nonce\' ); ?>
      <?php 
        $image = get_post_meta( $object->ID, \'upload_image\', true );
      ?>
      <div class="image-upload-block">
        <p>
            <input id="upload_image_<?php echo $i; ?>" type="text" size="100" name="upload_image" class="upload_image" value="<?php echo $image; ?>" />
            <img src="<?php echo $image; ?>" width="50"/>
            <input id="upload_image_button" class="upload_image_button" type="button" value="Upload Image" />
        </p>
        <p>
            <input id="upload_image" type="text" size="100" name="upload_image" class="upload_image" value="" />
            <input id="upload_image_button" class="upload_image_button" type="button" value="Upload Image" />
        </p>
      </div>
    <?php }

    function save_post_class_meta( $post_id, $post ) {
        /* Verify the nonce before proceeding. */
        if ( !isset( $_POST[\'post_class_nonce\'] ) || !wp_verify_nonce( $_POST[\'post_class_nonce\'], basename( __FILE__ ) ) )
            return $post_id;

        /* Get the post type object. */
        $post_type = get_post_type_object( $post->post_type );

        /* Check if the current user has permission to edit the post. */
        if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
            return $post_id;

        /* Get the posted data and sanitize it for use as an HTML class. */
        $new_meta_value = ( isset( $_POST[\'upload_image\'] ) ?  $_POST[\'upload_image\'] : \'\' );
        $new_meta_value = serialize($new_meta_value);
        /* Get the meta key. */
        $meta_key = \'upload_image\';

        /* Get the meta value of the custom field key. */
        $meta_value = get_post_meta( $post_id, $meta_key, true );

        /* If a new meta value was added and there was no previous value, add it. */
        if ( $new_meta_value && \'\' == $meta_value ){
            add_post_meta( $post_id, $meta_key, $new_meta_value, true );
        }

        /* If the new meta value does not match the old value, update it. */
        elseif ( $new_meta_value && $new_meta_value != $meta_value ){
            update_post_meta( $post_id, $meta_key, $new_meta_value );
        }

        /* If there is no new meta value but an old value exists, delete it. */
        elseif ( \'\' == $new_meta_value && $meta_value ){
            delete_post_meta( $post_id, $meta_key, $meta_value );
        }
    }
?>
您可以从WordPress Codex了解更多add meta box

结束

相关推荐