2015-04-23 63 views
0

我用update_option()函數添加複選框的WordPress:get_option檢索陣列

<input type="checkbox" name="BG[Choice_1]" value="Value_1" />1 
<input type="checkbox" name="BG[Choice_2]" value="Value_2" />2 
<input type="checkbox" name="BG[Choice_3]" value="Value_3" />3 

當我看wp_options數據庫表中的值,該字段的值是

a:3:{s:8:"Choice_1";s:7:"Value_1";s:8:"Choice_2";s:7:"Value_2";s:8:"Choice_3";s:7:"Value_3";}

我的問題是:

1)我怎麼簡單地echo數組值?我假設我需要使用get_option(),但我一直沒有成功

2)如果數據庫中的值是否存在,我該如何將複選框默認爲CHECKED?

回答

1

好吧......我想通了這一點......

<?php 
     $BG_options=get_option('BG'); 
     $BG_Choice_1_checked = $BG_options['Choice_1']; 
     $BG_Choice_2_checked = $BG_options['Choice_2']; 
     $BG_Choice_3_checked = $BG_options['Choice_3']; 
    ?> 

     <input type="checkbox" name="BG[Choice_1]" value="Value_1" <?php checked($BG_Choice_1_checked, "Value_1", "true"); ?> />Choice 1<br /> 
     <input type="checkbox" name="BG[Choice_2]" value="Value_2" <?php checked($BG_Choice_2_checked, "Value_2", "true"); ?> />Choice 2<br /> 
     <input type="checkbox" name="BG[Choice_3]" value="Value_3" <?php checked($BG_Choice_3_checked, "Value_3", "true"); ?> />Choice 3<br />