2

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.

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

  • 1
    What is <?php echo $option ?> supposed to do? – vancoder Jul 5 '13 at 16:44
  • Thanks vancoder, I use acera theme options, I am not too much of coder but it was in the function to generate options for each type. – Selma Jul 5 '13 at 21:38
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
    )
);
  • Thanks for your help, I will try it now, I really hope it works, you'll save my life. – Selma Jul 5 '13 at 21:42
  • it did not work, it showed the categories but they are out of the select, there is <option selected="selected" value=""> </option> out of the select. :( – Selma Jul 5 '13 at 22:13
  • I have edited my question by adding a useful detail, I really wish you guys can help me, I'm so thankful. – Selma Jul 5 '13 at 22:14
  • I'm sorry, but I don't see my answer not working properly... Of course, you have to wrap the above code in a select tags - as I stated, it's just the option part. And, as @Shahinul Islam pointed out, you have to provide a value to be saved! – tfrommen Jul 6 '13 at 22:46
  • I am a newbie, I copied the code you gave me inside the select tags I have, and I used the wp_dropdown_categories instead of get_categories.. but it showed nothing. The value that was saved is the cat_ID while the options in the back end show the cat_name.. everything was working except that the selected option is saved in database but if I refresh, the category is not selected. when I check its html code, I see no " selected=selected " . – Selma Jul 6 '13 at 23:21
1

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>
  • Thanks Shahinul, The code i and you sent back works fine, it's used to create a select type option. The value is created by the option array I use in the options page, I wanted to use it to create a select a category instead. – Selma Jul 6 '13 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.