更新:設置編程的<option>
在選擇字段:
1)您將需要存儲你的選擇關鍵值的關聯數組:
// The associative array to store (once)
$options_array = array(
'' => __('Select a value', 'woocommerce'), // default empty value
'one' => __('Regular', 'woocommerce'),
'two' => __('Nano', 'woocommerce'),
'three' => __('Micro', 'woocommerce')
);
// Serialize the array as a string
$option_str = maybe_serialize($options_array);
// Save this array in Wordpress options
update_option('my_custom_selector_options', $option_str);
2)獲取並反序列化您的選擇器選項:
// Get your options select data
$select_options_str = get_option('my_custom_selector_options');
// Unserialize this data:
$select_options_arr = maybe_unserialize($select_options_str);
// Get the saved selected 'value' if it exist
$value = get_post_meta($post->ID, '_select', true);
if(empty($value)) $value = ''; // When 'value' is not defined
//
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __('SIM Type', 'woocommerce'),
'options' => $select_options_arr,
'value' => $value,
)
);
因此,現在您的字段選擇器選項已被您從WordPress選項獲得的數據填充。
自動填寫的woocommerce_wp_select()
,你必須添加一個'value'
關鍵這樣:
## 1. The select (dropdown)
// Get the 'value' data if it exist
$value = get_post_meta($post->ID, '_select', true);
if(empty($value)) $value = ''; // When 'value' is not defined
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __('SIM Type', 'woocommerce'),
'options' => array(
'' => __('Select a value', 'woocommerce'), // Added a default empty value
'one' => __('Regular', 'woocommerce'),
'two' => __('Nano', 'woocommerce'),
'three' => __('Micro', 'woocommerce')
),
'value' => $value, // <=== === === === === HERE set the 'value' key (autofill)
)
);
## ---------------------------------
## 2. SAVING
$woocommerce_select = $_POST['_select'];
// The Default empty value is not saved (added in this condition below)
if(!empty($woocommerce_select) || $woocommerce_select != '')
update_post_meta($post_id, '_select', esc_attr($woocommerce_select));
快速測試:
要看到它在行動,取代例如:'value' => $value,
由'value' => 'two',
然後選擇值爲:納米 ...
感謝嗨@LoicTheAztec但我要問的是如何添加值,如「常規」,「納米」和「微「在選擇編程方式像一個for循環。以及如何存儲和獲取?它是否也通過meta?謝謝;) –
感謝您爲此,我會爲默認值執行此操作。 ;)它仍然有幫助。 ;) –
這是很好的去。 :) 謝謝! –