0

以下のコードを使用して、ワードプレスで投稿用のメタボックスを作成しました。しかし問題は、これらのメタ ボックスがカスタム フィールドにも表示されることです。これらのメタ ボックスをカスタム フィールドに表示したくありません。カスタムフィールドにメタボックスを表示する用途がないため、カスタムフィールドにメタボックスを表示しないようにする方法はありますか?

$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');
4

1 に答える 1

1

フィールド名の先頭にアンダースコアを付けると、非表示になります。そう:

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

コードの一部を少しクリーンアップすることもできます。update_post_meta が呼び出されると、フィールドが存在しない場合は自動的に追加されるため、このコンテキストでは add_post_meta 呼び出しは不要です。これを次のいずれかに要約することを検討してください。

if($data && $data != get_post_meta($post_id, '_'.$meta_box['name'].'_value', true))  
    update_post_meta($post_id, '_'.$meta_box['name'].'_value', $data); //Runs if $data exists and is different
else
    delete_post_meta($post_id, '_'.$meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));

またはこれ:

if($data)  
    update_post_meta($post_id, '_'.$meta_box['name'].'_value', $data); //Runs if $data exists
else
    delete_post_meta($post_id, '_'.$meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
于 2012-05-15T17:52:06.177 に答える