4

我在 Wordpress 中使用 WooCommerce 插件。在该插件中,我需要为每个产品添加文本输入,客户可以在其中填写其他产品详细信息。

我面临的问题是,每当我添加文本属性时,它都会变成一个选择框。请提出一些解决方案。谢谢!

4

1 回答 1

-2

你的意思是你的客户应该在前端有这个文本输入字段吗?

如果它只是后端,您可以将其添加到您的functions.php:

if (is_admin()){     
// Add the Meta Box  
function add_textfield() {  
    add_meta_box(  
        'textfield', // $id  
        'Custom textfield', // $title   
        'show_textfield_backend', // $callback  
        'product', // $page  
        'normal', // $context  
        'high'  // $priority
    ); 
}  
add_action('add_meta_boxes', 'add_textfield');

// The Callback  
function show_textfield_backend() {  
    global $post;  
    global $wpdb;

    $meta = get_post_meta($post->ID, 'textfield', true);

    // Use nonce for verification  
    echo '<input type="hidden" name="textfield_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  

    _e('Custom textfield: ');
    echo '<input type="text" placeholder="text" id="textfield" class="textfield" name="textfield" ', $meta ? " value='$meta'" : "",' />';       
}

// Save the Data  
function save_textfield($post_id) {  
    global $wpdb;

    // verify nonce  
    if (!wp_verify_nonce($_POST['textfield_nonce'], basename(__FILE__)))   
        return $post_id;
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
    } elseif (!current_user_can('edit_post', $post_id)) {  
        return $post_id;  
    }

    if ( !wp_is_post_revision( $post_id ) ) {

        $textfield = $_POST['textfield'];
        update_post_meta($post_id, 'textfield', $textfield);

    }  
}
add_action('save_post', 'save_textfield');
}
于 2013-06-04T12:27:26.513 回答