将相同的自定义元框分配给多个帖子类型

时间:2013-10-04 作者:Neelam Khan

我目前将视频设置为自定义帖子类型,并为其创建了一个自定义元框,允许用户输入youtube/vimeo视频的ID,然后视频将显示在前端。

我想将此元框重新用于另一个自定义帖子类型。我该怎么做?

当前元框的功能是:

// Create the Video Information Meta Box by hooking into the admin menu for a post
    add_action(\'admin_menu\', \'video_add_box\');


    function video_add_box(){
    add_meta_box(\'video_information\', \'Video Information\', \'video_information\', \'videos\', \'normal\', \'high\');
    }

    //function to populate the meta box added above
    function video_information(){
    global $post;

    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="video_noncename" id="video_noncename" value="\' .
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    //adds the custom field _youtubeID plus some other stuff
    $youtubeID = get_post_meta($post->ID, \'_youtubeID\', true);
    if ( empty($youtubeID) ) {
    $youtubeID = \'\';
    }

    //adds the custom field _vimeoID
    $vimeoID = get_post_meta($post->ID, \'_vimeoID\', true);
    if ( empty($vimeoID) ) {
    $vimeoID = \'\';
    }

    //add the box
    echo \'<br />\';
    echo \'<strong>Youtube ID:</strong>  <input type="text" name="_youtubeID" value="\' . $youtubeID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    echo \'<strong>Vimeo ID:</strong>  <input type="text" name="_vimeoID" value="\' . $vimeoID  . \'" size="20" maxlength="30" />\';
    echo \'<br />\';
    } //end video_information function

    //save_video_meta is called below with the action "save_post" and saves your IDs to the post
    function save_video_meta($post_id, $post) {
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times

    if ( !wp_verify_nonce( $_POST[\'video_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?

    if ( !current_user_can( \'edit_post\', $post->ID )){
    return $post->ID;
    }

    $video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
    $video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
    foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
    if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice

    $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV

    if($value) {
    update_post_meta($post_id, $key, $value);
} else 
    delete_post_meta($post_id, $key); // Delete if blank
}
    } //end save_video_meta

    //save the video custom fields
    add_action( \'save_post\', \'my_save_postdata\' );
function my_save_postdata($post_id){
    $video_meta[\'_youtubeID\'] = $_POST[\'_youtubeID\'];
    $video_meta[\'_vimeoID\'] = $_POST[\'_vimeoID\'];
    foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
        if(  $_POST[\'post_type\'] == \'revision\' ) return; // Don\'t store custom data twice
        $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV

        if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post_id, $key, $value);
        } else { // If the custom field doesn\'t have a value
            add_post_meta($post_id, $key, $value);
        }
        if(!$value) delete_post_meta($post_id, $key); // Delete if blank
    }//endforeach video meta
 }

2 个回复
最合适的回答,由SO网友:KalymaStudio 整理而成

是的,您需要编辑video\\u add\\u box()函数,以便生成一个具有显示该框所需的post类型的数组。

$postypes = array(\'type1\', \'type2\', \'type3\');
foreach ( $postypes as $postype) {

    add_meta_box(
        \'video_information\',
        \'Video Information\',
        \'video_information\',
        $postype
    );
}
您可以在codex中阅读更多内容:http://codex.wordpress.org/Function_Reference/add_meta_box

SO网友:Mohit Bumb
add_action( \'add_meta_boxes\', \'my_add_custom_box\' );

function my_add_custom_box($postType) {
    $types = array(\'type1\', \'type2\', \'type3\');
    if(in_array($postType, $types)){
        add_meta_box(
                \'myid\',
                __( \'Title\', \'myplugin_textdomain\' ),
                \'callback\',
                $postType
        );
    }
}

Source : http://wordpress.org/ideas/topic/add-meta-box-to-multiple-post-types

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register