Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Terms of custom taxonomy in a dropdown menu for meta options box WordPress

  • SOLVED

Hey everyone,

I need some help adding a dropdown menu of the terms of a custom taxonomy to a meta options box I'm working on. I'm expanding the functionality of my theme's feature slider to allow users to select the category they want their custom slides to call from on a per page basis. I have the functionality in place, and I have tested it using a simple text input field in a meta options box. However, I would like to use a dropdown menu of the categories rather than make the user enter in the category name manually.

Here are the two snippets of code I am working with:

function show_field_select($field, $meta) {
if (!is_array($meta)) $meta = (array) $meta;
$this->show_field_begin($field, $meta);
echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
foreach ($field['options'] as $key => $value) {
echo "<option value='$key'" . selected(in_array($key, $meta), true, false) . ">$value</option>";
}
echo "</select>";
$this->show_field_end($field, $meta);



and

$meta_boxes[] = array(
'id' => 'page',
'title' => 'Page Meta Options',
'pages' => array('page'),

'fields' => array(

array(
'name' => 'Slide Category',
'desc' => 'Enter your slide category here',
'id' => $prefix . 'category',
'type' => 'text',
'std' => ''
),

array(
'name' => 'Slide Category Dropdown',
'desc' => 'Enter your slide category here',
'id' => $prefix . 'category_dropdown',
'type' => 'select',
'options' => array('test1', 'test2', 'test3'),
'std' => ''
),
)
);


The first defines the type "select" which you can see in use in the second code snippet (the first array was the one I used with a simple text input to test calling a category on a per-page basis and it works). I have been using this for a different meta options box where I have manually input the options, but now I would like the options to be populated by the categories of this custom taxonomy instead of the "test1 test2 and test3" I have as a simple placeholder .

The custom taxonomy is called slide_categories, and I double checked that everything is in place by throwing the following on my index page to test:

<?php wp_dropdown_categories('show_option_none=Select category&orderby=name&echo=1&taxonomy=slide_categories'); ?>

In short, I need the dropdown to populate the different categories of this custom taxonomy, and I need it to save the category name as the value (for example, if the category is about it need to save the value "about".

Answers (1)

2011-06-16

Utkarsh Kukreti answers:

Add this before your $meta_boxes = ... code

$terms = get_terms('slide_categories', 'hide_empty=0');
$options = array();
foreach($terms as $term) {
$options[$term->slug] = $term->name;
}


And change

'options' => array('test1', 'test2', 'test3'),

to

'options' => $options,


Tyler Cunningham comments:

That did not seem to work, if it helps the output of $options is Array and the output of $terms is blank so get_terms('slide_categories'); doesn't appear to be the right call for that variable.


Utkarsh Kukreti comments:

Ah. Your taxonomies may be having 0 posts then. Please try the updated code.


Tyler Cunningham comments:

Both categories in slide_categories have 1 post each, updated code made no difference.


Utkarsh Kukreti comments:

What does var_dump($terms); print?


Tyler Cunningham comments:

object(WP_Error)#199 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid Taxonomy" } } ["error_data"]=> array(0) { } }


Utkarsh Kukreti comments:

And for this?

var_dump(get_taxonomies());


Tyler Cunningham comments:

array(5) { ["category"]=> string(8) "category" ["post_tag"]=> string(8) "post_tag" ["nav_menu"]=> string(8) "nav_menu" ["link_category"]=> string(13) "link_category" ["post_format"]=> string(11) "post_format" }


Utkarsh Kukreti comments:

It seems like you're registering the taxonomy after this code is run. I'll probably have to look at the whole theme code to confirm.


Tyler Cunningham comments:

Email me at [email protected], I will send you a zip of the current state of the theme code. I don't think it's getting called after this code though, I define the taxonomy in my functions.php and then include the meta options file at the bottom, but I could be wrong.

Thank you for the help, it's greatly appreciated.


Utkarsh Kukreti comments:

Email sent.


Tyler Cunningham comments:

Issue was resolved offline, Utkarsh was a great help!