创建不显示在自定义字段中的元框

时间:2012-05-15 作者:inderpreet singh

我使用下面的代码为帖子创建了一个元框。这些元框也可以在自定义字段中看到。

我不希望这些元框显示在自定义字段中。有没有办法不在自定义字段中显示元框?

$meta_boxes_video = 
array(
"hotel name1" => array(
"name" => "hotel_name1",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price1" => array(
"name" => "price1",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name2" => array(
"name" => "hotel_name2",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price2" => array(
"name" => "price2",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name3" => array(
"name" => "hotel_name3",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price3" => array(
"name" => "price3",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name4" => array(
"name" => "hotel_name4",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price4" => array(
"name" => "price4",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name5" => array(
"name" => "hotel_name5",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price5" => array(
"name" => "price5",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"hotel name6" => array(
"name" => "hotel_name6",
"type" => "input",
"title" => "<strong>Hotel Name</strong> (Required)",
"description" => ""),
"price6" => array(
"name" => "price6",
"type" => "input",
"title" => "<strong>Price</strong>",
"description" => ""),
"img_name1" => array(
"name" => "img_name1",
"type" => "input",
"title" => "<strong>Img1</strong> (Required)",
"description" => ""),
"img_name2" => array(
"name" => "img_name2",
"type" => "input",
"title" => "<strong>img2</strong> (Required)",
"description" => ""),
);


function display_video_meta_boxes() {
global $post, $meta_boxes_video;
    foreach($meta_boxes_video as $meta_box) {

        echo\'<input type="hidden" name="\'.$meta_box[\'name\'].\'_noncename" id="\'.$meta_box[\'name\'].\'_noncename" value="\'.wp_create_nonce( plugin_basename(__FILE__) ).\'" />\';

        echo\'<h2>\'.$meta_box[\'title\'].\'</h2>\';

        if( $meta_box[\'type\'] == "input" ) { 

            $meta_box_value = get_post_meta($post->ID, $meta_box[\'name\'].\'_value\', true);

            if($meta_box_value == "")
                $meta_box_value = $meta_box[\'std\'];
            $meta_box_value = str_replace("\\"","\'",$meta_box_value);

            echo\'<input type="text" name="\'.$meta_box[\'name\'].\'_value" value="\'.$meta_box_value.\'" size="55" /><br />\';

        } elseif( $meta_box[\'type\'] == "textarea" ) { 

            $meta_box_value = get_post_meta($post->ID, $meta_box[\'name\'].\'_value\', true);

            if($meta_box_value == "")
                $meta_box_value = $meta_box[\'std\'];
            $meta_box_value = str_replace("\\"","\'",$meta_box_value);

            echo\'<textarea name="\'.$meta_box[\'name\'].\'_value" value="\'.$meta_box_value.\'" style="width:100%" cols="20" rows="1">\'.$meta_box_value.\'</textarea><br />\';

        } elseif( $meta_box[\'type\'] == "button" ) { 

            $meta_box_value = "Add New Source";

            if($meta_box_value == "")
                $meta_box_value = $meta_box[\'std\'];
            $meta_box_value = str_replace("\\"","\'",$meta_box_value);
            echo \'<script type="text/javascript">
            function test()
            {
            alert("fgfg");
            var element = document.createElement("textarea");
            element.setAttribute("value", "Dfdsf");
            element.setAttribute("name", "sdfdf");
            var foo = document.getElementById("foo");
            foo.appendChild(element);
            }
            </script>\';
            echo \'<div id="foo">\';
            echo\'<input type="button" name="\'.$meta_box[\'name\'].\'" value="\'.$meta_box_value.\'" onclick="test();"  /><br /></div>\';

        }
        elseif ( $meta_box[\'type\'] == "select" ) {

            echo\'<select name="\'.$meta_box[\'name\'].\'_value">\';

            foreach ($meta_box[\'options\'] as $option) {

                echo\'<option\';
                if ( get_post_meta($post->ID, $meta_box[\'name\'].\'_value\', true) == $option ) { 
                    echo \' selected="selected"\'; 
                } elseif ( $option == $meta_box[\'std\'] ) { 
                    echo \' selected="selected"\'; 
                } 
                echo\'>\'. $option .\'</option>\';

            }

            echo\'</select>\';

        } elseif ($meta_box[\'type\'] == "info") {


            echo \'<p><strong>Add your property images using the "Upload/Insert" button above the content textbox..</p>\';
        }

        echo\'<p><label for="\'.$meta_box[\'name\'].\'_value">\'.$meta_box[\'description\'].\'</label></p>\';
    }

}

function create_video_meta_box() 
{
    if (function_exists(\'add_meta_box\')) 
    {
        add_meta_box( \'ameta_boxes\', \'Extra Information\', \'display_video_meta_boxes\', \'post\', \'normal\', \'high\' );
    }
}

function save_video_postdata( $post_id ) {
    global $post, $meta_boxes_video;  

    if( get_post_type() == \'post\' ) {
    foreach($meta_boxes_video as $meta_box) {  

        // Verify  
        if ( !wp_verify_nonce( $_POST[$meta_box[\'name\'].\'_noncename\'], plugin_basename(__FILE__) )) {  
        return $post_id;  
        }  

        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;  
        }  

        $data = stripslashes($_POST[$meta_box[\'name\'].\'_value\']);  

        if(get_post_meta($post_id, $meta_box[\'name\'].\'_value\') == "")  
        add_post_meta($post_id, $meta_box[\'name\'].\'_value\', $data, true);  
        elseif($data != get_post_meta($post_id, $meta_box[\'name\'].\'_value\', true))  
        update_post_meta($post_id, $meta_box[\'name\'].\'_value\', $data);  
        elseif($data == "")  
        delete_post_meta($post_id, $meta_box[\'name\'].\'_value\', get_post_meta($post_id, $meta_box[\'name\'].\'_value\', true));  
        }
    }

}


add_action(\'admin_menu\', \'create_video_meta_box\');
add_action(\'save_post\', \'save_video_postdata\');

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

所有post meta都显示在自定义字段中,除非它以下划线开头。

因此,要从自定义字段元框中“删除”post meta,只需给它一个名称,以下划线开头。然后,您将负责保存/更新帖子元(必要时添加您自己的元盒)。

结束

相关推荐

如何在不破坏TinyMCE的情况下在metabox帖子页面上使用媒体上传?

我使用以下代码在帖子或页面管理部分的多个mextabox中上传。适用于元盒,但会打断TinyMCE插入图像。我想知道我怎样才能做到这一点,而又不致于打破僵局。当我覆盖窗口时,问题就出现了。send\\u to\\u editor函数。jQuery(document).ready(function() { jQuery(\'.st_upload_button\').click(function() { targetfield = jQuery(this).prev(