How to mark an option as selected

I want to display post categories in theme settings, and I successfully did that using this code:

<?php /* Get the list of categories */ 
            $categories = get_categories();
            foreach ( $categories as $category) :
            $selected = ''; 
            ?>
<option <?php echo $option ?> value="<?php echo $category->cat_ID; ?>">
<?php echo $category->cat_name; ?></option>
<?php endforeach; ?>

Everything is working fine and I’m able to call the option.

Read More

What is missing is that after I select a category, then I save the settings the category is not marked as selected within the drop down box. Yet the selected category is saved in the database.

I’m using acera theme options, and I edited the select type to create a select category option.

here is the code I started with:

<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
                        <?php
                        if (get_option($value['id']))
                            $default = get_option($value['id']);
                        else
                            $default = $value['default'];

                        foreach ($value['options'] as $option):
                            $selected = '';
                            if ($option == $default)
                                $selected = ' selected="selected"';
                            ?>
                            <option <?php echo $selected; ?>><?php echo $option ?>

                            </option>
                        <?php endforeach; ?>


                    </select>

So from this one I want to display the categories (I did this and the options are working in front end) and also to set the selected one as selected. (This is missing)

Thanks

Related posts

2 comments

  1. What you are looking for is the selected() function.

    Your updated option should look like this:

    <option
        value="<?php echo $category->cat_ID; ?>"
        <?php selected($option, $category->cat_ID); ?>>
        <?php echo $category->cat_name; ?>
    </option>
    

    // EDIT
    Oh, and BTW, in your particular case you might want to use the wp_dropdown_categories function, I guess.

    That would be:

    wp_dropdown_categories(
        array(
            'selected' => $option
            // maybe some other settings
        )
    );
    
  2. Please check your HTML code you don’t give option’s value : so when you store it will not saved.

    <select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
                        <?php
                        if (get_option($value['id']))
                            $default = get_option($value['id']);
                        else
                            $default = $value['default'];
    
                        foreach ($value['options'] as $option):
                            $selected = '';
                            if ($option == $default)
                                $selected = ' selected="selected"';
                            ?>
                            <option <?php echo $selected; ?> value="<?php echo $option ?>"><?php echo $option ?>
    
                            </option>
                        <?php endforeach; ?>
    
    
                    </select>
    

Comments are closed.