I’ve got a select dropdown which outputs all the available categories of the WordPress site. Now I’ve been trying to also set the value of each option to the category name. This is my code:

<select id="test">
   <option value="one">Select Case Study Category</option>
     <?php $categories =  get_categories('');  
      foreach  ($categories as $category) {
       echo '<option value='.$category->name.'>'.$category-
       >name.'</option> ';
      }
    ?>

This works for a couple of categories such as “Uncategorised” but it doesn’t work for categories with a space in them so for example “Case Study”. For those categories it sets the value as
So it obviously seems to be an issue with the spaces, but anybody got any idea how I can fix this?

Read more here: WordPress – get category name as select option value


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: