Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You can use the standard WordPress function, get the dropdown already formatted and solve both problems at once.

Like so:

define( 'WP_USE_THEMES', false );
require( './wp-load.php' );

wp_dropdown_categories(
    array(
        'child_of' => 0,
        'class' => 'postform', 
        'depth' => 0,
        'echo' => 1,
        'exclude' => '', 
        'hide_empty' => false, 
        'hide_if_empty' => false,
        'hierarchical' => true,
        'id' => '',
        'name' => 'cat-dropdown', 
        'order' => 'ASC',
        'orderby' => 'name', 
        'selected' => 0, 
        'show_count' => 0,
        'show_option_all' => '', 
        'show_option_none' => __('None'),
        'tab_index' => 0, 
        'taxonomy' => 'category',
    )
);

Outputs:

<select name='cat-dropdown' id='cat-dropdown' class='postform' >
    <option value='-1'>None</option>
    <option class="level-0" value="2">Other</option>
    <option class="level-0" value="1">Uncategorized</option>
    <option class="level-1" value="4">&nbsp;&nbsp;&nbsp;Other</option>
    <option class="level-2" value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Other</option>
</select>

For the default category: categories list

share|improve this answer
    
I get no output from this, just a white page. –  Sweepster Jan 14 '13 at 3:14
    
Figured out the echo option kills the dropdown. Set it to 1 and all's good! –  Sweepster Jan 14 '13 at 3:30
    
Oh, that was a left over from testing, answer updated... it's used for $holder = wp_dropdown_categories($args); –  brasofilo Jan 14 '13 at 3:37
    
Gr8 job mate - thanks a lot –  Sagive SEO Feb 8 '13 at 15:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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