0

我使用下面的代码为 wordpress 中的帖子创建了一个元框。但问题是这些元框也出现在自定义字段中。我不希望这些元框显示在自定义字段中。有什么方法可以避免在自定义字段中不显示元框,因为在自定义字段中显示它是没有用的

$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 回答