在自定义帖子中上传WordPress metabox文件

时间:2014-07-02 作者:Sohail

我对wordpres的开发相对来说比较陌生,我一直都在关注这个问题。我已经创建了一个自定义的帖子类型,它显示在管理菜单中。我需要一个带有文件上传字段的元数据库。metabox显示良好,但未保存文件。

我正在使用以下代码。任何帮助都将不胜感激。

//////////////////Best Practices///////////////

add_action( \'init\', \'wpb_register_cpt_best_practices\' );

function wpb_register_cpt_best_practices() {

    $labels = array(
        \'name\' => _x( \'Best Practices\', \'best practices\' ),
        \'singular_name\' => _x( \'Best Practice\', \'best practice\' ),
        \'add_new\' => _x( \'Add New\', \'best practice\' ),
        \'add_new_item\' => _x( \'Add New best practice\', \'best practice\' ),
        \'edit_item\' => _x( \'Edit best practice\', \'best practice\' ),
        \'new_item\' => _x( \'New best practice\', \'best practice\' ),
        \'view_item\' => _x( \'View best practice\', \'best practice\' ),
        \'search_items\' => _x( \'Search best practices\', \'best practice\' ),
        \'not_found\' => _x( \'No best practices found\', \'best practice\' ),
        \'not_found_in_trash\' => _x( \'No best practices found in Trash\', \'best practice\' ),
        \'parent_item_colon\' => _x( \'Parent best practice:\', \'best practice\' ),
        \'menu_name\' => _x( \'Best practices\', \'best practice\' ),
    );

    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => false,

        \'supports\' => array( \'title\', \'editor\',  \'revisions\' ),

        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\'
    );

    register_post_type( \'best practice\', $args );
}
$key_bp = "best practice";
$meta_boxes_bp = array(

    "document" => array(
        "name" => "document",
        "title" => "Best Practices Document",
        "description" => "Attach the best practices document.",
        "type" => "file")

);

function wpb_create_meta_box3() {
    global $key_bp;

    if( function_exists( \'add_meta_box\' ) ) {
        add_meta_box( \'new-meta-boxes\', ucfirst( $key_bp ) . \' Document\', \'display_meta_box3\', \'best practice\', \'normal\', \'high\' );
    }
}

function display_meta_box3() {
    global $post, $meta_boxes_bp, $key_bp;
    ?>

    <div class="form-wrap">

        <?php
        wp_nonce_field( plugin_basename( __FILE__ ), $key_bp . \'_wpnonce\', false, true );

        foreach($meta_boxes_bp as $meta_box) {
            $data = get_post_meta($post->ID, $key_bp, true);
            ?>

            <div class="form-field form-required">
                <label for="<?php echo $meta_box[ \'name\' ]; ?>"><?php echo $meta_box[ \'title\' ]; ?></label>
                <input type="file" name="<?php echo $meta_box[ \'name\' ]; ?>" value="<?php echo (isset($data[ $meta_box[ \'name\' ] ]) ? $data[ $meta_box[ \'name\' ] ]  : \'\'); ?>" />
                <p><?php echo $meta_box[ \'description\' ]; ?>
                    <?php //print_r($data);
                    ?></p>
            </div>

        <?php } ?>

    </div>
<?php
}

function wpb_save_meta_box3( $post_id ) {
    global $post, $meta_boxes_bp, $key_bp;

    foreach( $meta_boxes_bp as $meta_box ) {
        if (isset($_POST[ $meta_box[ \'name\' ] ])) {
            $data[ $meta_box[ \'name\' ] ] = $_POST[ $meta_box[ \'name\' ] ];
        }
    }

    if (!isset($_POST[ $key_bp . \'_wpnonce\' ]))
        return $post_id;

    if ( !wp_verify_nonce( $_POST[ $key_bp . \'_wpnonce\' ], plugin_basename(__FILE__) ) )
        return $post_id;

    if ( !current_user_can( \'edit_post\', $post_id ))
        return $post_id;

    update_post_meta( $post_id, $key_bp, $data );
}

add_action( \'admin_menu\', \'wpb_create_meta_box3\' );
add_action( \'save_post\', \'wpb_save_meta_box3\' );

1 个回复
最合适的回答,由SO网友:TBI Infotech 整理而成

在自定义贴子中上载Wordpress metabox文件。请参考以下链接

http://code.tutsplus.com/articles/attaching-files-to-your-posts-using-wordpress-custom-meta-boxes-part-1--wp-22291

或者您可以使用以下插件

https://wordpress.org/plugins/simple-fields/
http://www.advancedcustomfields.com/

这两个插件最适合自定义字段。您还可以创建可重复的字段

结束

相关推荐

显示Metabox值(自定义帖子类型分类)

我已经在这上面呆了好几天了,读了一篇又一篇的文章,什么都不管用。我正在尝试在模板页面上显示我的metabox的值。我成功地添加了元盒,并且在我的页面管理中看到了选择框,它似乎也保存得很好。我似乎无法在模板页面上回显元数据库中选定的类别。我读到的每一个地方都说使用以下对我不起作用的内容。<?php echo get_post_meta( $post->ID, \'sm_taxonomy\', true ); ?> 下面是我的其余代码add_filter( \'r