2014-01-16 52 views
0

而不是僅選中當前值,它們全部被選中。當我回顯當前值時,它顯示正確,但我似乎無法弄清楚如何讓它被選中。選擇當前下拉值 - WordPress主題選項

<?php 
echo $settings['featured-one']; 
$cats = get_categories(); 
echo '<select name="sa_options[featured-one]"><option value="select">Select</option><option value="empty">Categories</option>'; 
foreach($cats as $cat){ 
    if ($cat->term_id.$cat->slug == $settings['featured-one']) $selected = 'selected="selected"'; 
    echo '<option value="'.$cat->term_id.$cat->slug.'" '.$selected.'> - '.$cat->name.'</option>'; 
} 
$pgs = get_pages(); 
echo '<option value="empty">Pages</option>'; 
foreach($pgs as $pg){ 
    if ($pg->ID.$pg->post_name == $settings['featured-one']) $selected = 'selected="selected"'; 
    echo '<option value="'.$pg->ID.$pg->post_name.'" '.$selected.'> - '.$pg->post_title.'</option>'; 
} 
$posts = get_posts(); 
echo '<option value="empty">Posts</option>'; 
foreach($posts as $post){ 
    if ($post->ID.$post->post_name == $settings['featured-one']) $selected = 'selected="selected"'; 
    echo '<option value="'.$post->ID.$post->post_name.'" '.$selected.'> - '.$post->post_title.'</option>'; 
} 
echo '</select>'; 
?> 
+0

檢查元素和檢查是否得到'selected = selected'? – Rikesh

+0

每個選項都獲取selected = selected。 – lefty55104

+0

有問題。在每個if條件之前定義'$ selected ='';'。 – Rikesh

回答

0

按照上面的評論,我每天if語句前放置$selected = '';

0

嘗試WordPress的默認技術,而不是我們自己的

參考,鏈接selected function

<!-- Testing the values with if() --> 
<select name="options[foo]"> 
    <option value="1" <?php if ($options['foo'] == 1) echo 'selected="selected"'; ?>>1</option> 
    <option value="2" <?php if ($options['foo'] == 2) echo 'selected="selected"'; ?>>2</option> 
    <option value="3" <?php if ($options['foo'] == 3) echo 'selected="selected"'; ?>>3</option> 
</select> 

<!-- Using selected() instead --> 
<select name="options[foo]"> 
    <option value="1" <?php selected($options['foo'], 1); ?>>1</option> 
    <option value="2" <?php selected($options['foo'], 2); ?>>2</option> 
    <option value="3" <?php selected($options['foo'], 3); ?>>3</option> 
</select>