2013-08-04 88 views
1

我有一個複選框,其默認狀態爲未選中:WordPress的設置頁面,如何默認複選框的狀態來檢查?

<?php 
function edit_theme_settings() { 
    if (get_option('sold_text') == true) { $display = 'checked'; } 
    else { $display = ''; } 
    update_option('sold_text', $display); 
?> 

<input type="checkbox" name="sold_text" id="sold_text" <?php echo get_option('sold_text'); ?> /> 

我想它的默認狀態爲選中第一時間顯示的形式,隨後其「選中」狀態應該由定義get_option( 'sold_text')。

+0

和功能'get_option()'? – 2013-08-04 21:18:48

+0

get_option()呢? – Andy

回答

3

既不建議的工作對我來說在這種情況下,但我想我已經通過使用add_option()

自己解決它一個安全的方式,將一個命名的選項/值對添加到選項數據庫表。 如果該選項已存在,則不執行任何操作

所以我做了:add_option('sold_text')的值'checked',因此該複選框默認爲選中狀態。現在,由於選項已經存在add_option()什麼也不做下一次加載窗體或提交和update_option()處理的複選框狀態更新中...

<?php 
function edit_theme_settings() { 

add_option('sold_text', 'checked'); 

if (get_option('sold_text') == true) { $display = 'checked'; } 
else { $display = ''; } 
update_option('sold_text', $display); 
?> 

<input type="checkbox" name="sold_text" id="sold_text" <?php echo get_option('sold_text'); ?> /> 
1

複選框被存儲爲0或1(爲未選中,選中),所以你需要的東西是這樣的:

<input type="checkbox" name='sold_text' id='sold_text' value="1" <?= checked(get_option('sold_text'), 1, false);?> /> 

WP的檢查()函數是專爲這樣的:http://codex.wordpress.org/Function_Reference/checked

相關問題