douping1581 2014-04-25 11:16
浏览 16
已采纳

如何设置我的默认选项?

I have a theme options page and a loop which grabs my categories. This code works fine and I am able to save my options. The code like this:

function drop_elements_function(){
$my_cats = get_categories(); 
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
    'value' => $my_cat->cat_ID,
    'label' => $my_cat->cat_name
);
$i++;
endforeach;

$options = get_option('sandbox_theme_social_options');

echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';

foreach ( $my_categories as $category ){
    $label = $category['label'];
    $selected = '';
    if ( $options['Drop_Elements'] == $category['value'] )
        $selected = 'selected="selected"';
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
    }
echo '</select>';
 //print_r($options['Drop_Elements']);
}/*Function end*/

The output looks like this:
http://www.vasinternetposao.com/img.png

Problem: Now when my theme is installed for the first time i am getting the output like the screen shot above but obviously my option is not yet saved to the database (user must click the save button in order to select that category).

So i was thinking to do something like this:
1.) User install the theme for the first time and then output looks like this:
http://www.vasinternetposao.com/img2.png
2.) If user unintentionally select "Choose your category" and clicks the "save button" the output will be again:
http://www.vasinternetposao.com/img2.png
3.) If the user select the real category (not "Choose Your Category") then "Choose Your Category" Disappears:
http://www.vasinternetposao.com/img.png

This is my attempt but it is not working:

function drop_elements_function(){
$my_cats = get_categories(); 
$i = 1;
foreach( $my_cats as $my_cat ) :
$my_categories[$my_cat->cat_ID] = array(
    'value' => $my_cat->cat_ID,
    'label' => $my_cat->cat_name
);
$i++;
endforeach;

$options = get_option('sandbox_theme_social_options');

echo'<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';

foreach ( $my_categories as $category ){
    $label = $category['label'];
    $selected = '';
    if ( $options['Drop_Elements'] == $category['value'] ){
        $selected = 'selected="selected"';
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
    }
        elseif(!isset($options['Drop_Elements'])){
        $selected = 'selected="selected"';
        echo '<option selected="selected"  value="Choose Your Category">Choose Your Category</option>'; 
        echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . '>' . $label . '</option>';

        }   

}/*Foreach close*/
echo '</select>';
 //print_r($options['Drop_Elements']);
}/*Function end*/

Can it be done with PHP? Any help is appreciated! Thank you!

  • 写回答

1条回答 默认 最新

  • dongzhanbi0027 2014-04-25 11:38
    关注

    I think what you want to do is something like the following example:

    echo '<select id="featured_cat" name="sandbox_theme_social_options[Drop_Elements]">';    
    echo '<option selected="selected"  value="Choose Your Category">Choose Your Category</option>'; 
        foreach ( $my_categories as $category ){
            $label = $category['label'];
            $selected = ( $options['Drop_Elements'] == $category['value'] ) ? " selected='selected' " : "";
            echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
    
        }
    echo "</select>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP