I have a form that contains a dropdown menu. This form is on a page form.php
and is not part of my theme. It exists outside of my site. This dropdown menu contains my list of values for a custom taxonomy called Formats. My Formats are as follows:
Entry Form
Facebook
- Entry Form
- Page
Twitter
This is my code so far:
<?php include("wp-blog-header.php"); ?>
<?php
global $wpdb;
$getFormats = $wpdb->get_results($wpdb->prepare("
SELECT * FROM wp_terms p
LEFT OUTER JOIN wp_term_taxonomy t ON p.term_id = t.term_id
WHERE t.taxonomy = 'format'
")); ?> // This gets all the values for the format taxonomy
<form> //start form
<select name="format"> //start dropdown
<?php
foreach ($getFormats as $format) { //spit out the formats
echo "<option value='".$format->name."'>".$format->name."</option>";
} ?>
</select> //end dropdown
</form> // end form
?>
So far, my dropdown looks like this:
Entry Form
Facebook
Entry Form
Page
Twitter
First question: How can I get the hierarchy to stick (i.e. keep the indents)?
My HTML output is as follows:
<option value="Entry Form">Entry Form</option>
<option value="Facebook">Facebook</option>
<option value="Entry Form">Entry Form</option> // <---- this is gonna be a problem
<option value="Page">Page</option>
<option value="Twitter">Twitter</option>
Second Question: You’ll notice the output for the second instance of Entry Form is identical to the first instance. Obviously, this is going to be a problem for submitting the form. How can I make this unique?
You can use the standard WordPress function, get the dropdown already formatted and solve both problems at once.
Like so:
Outputs:
For the default category: