2016-11-13 37 views
0

我做了搜索,發現這個一些例子和答案,但不是太多的編碼器的檢查,我真的沒有因爲代碼看起來比我的不同理解的解決方案。WordPress的PHP複選框默認

我有了在添加後頁面的幾個選項複選框一個WP主題。我想要默認選中一個複選框:

array(
     'name' => 'Show in Front Page Heading Slider', 
     'id' => $prefix . 'fps', 
     'type' => 'checkbox' 
    ), 

顯然我需要在這裏添加選中的值,但是我的嘗試沒有奏效。有任何想法嗎?

+0

你有沒有試過''檢查'=>'checked''? – jonogilmour

+0

謝謝,我在發佈之前嘗試過。它沒有工作:(希望它有工作。 – Herb

回答

0

我認爲你需要這樣的:<?php checked($checked, $current, $echo); ?>

<?php 

// Get an array of options from the database. 
$options = get_option('slug_option'); 

// Get the value of this option. 
$checked = $options['self-destruct']; 

// The value to compare with (the value of the checkbox below). 
$current = 1; 

// True by default, just here to make things clear. 
$echo = true; 

?> 
<input name="slug-option[self-destruct]" value="1" <?php checked($checked, $current, $echo); ?>/> 

與IF()測試值:

<input type='checkbox' name='options[postlink]' value='1' <?php if (1 == $options['postlink']) echo 'checked="checked"'; ?> /> 

使用檢查()代替:

<input type="checkbox" name="options[postlink]" value="1" <?php checked($options['postlink'], 1); ?> /> 

上看看此鏈接更多信息:https://codex.wordpress.org/Function_Reference/checked

+0

這看起來很有希望,但我知道的太少了,我不知道如何實現,難道我補充一點,整個PHP語句的頭,然後在添加的輸入型數組? – Herb

+0

也許有一個更簡單的解決方案。也許我設置數據庫字段默認爲任何指示檢查...我顯示的代碼是可以選擇的10個選項的一部分,我只檢查一個選項,這可能是一個痛苦 – Herb

+0

通過查看數據庫,我還沒有找到檢查狀態字段... – Herb