List taxonomies in a dropdown in theme's option panel
-
16-10-2019 - |
Question
I want to show a dropdown in the option's panel for choosing a "featured product category" which is a custom taxonomy. I do this way:
register_taxonomy("Catalogs",
array("kmproduct"),
array("hierarchical" => true,
"label" => "Catalogs",
"singular_label" => "catalog",
"rewrite" => true ));
and in my theme_options.php
i have:
...
array( "name" => "Homepage featured category",
"desc" => "Choose a category from which featured posts are drawn",
"id" => $shortname."_feat_cat",
"type" => "select",
"options" => $wp_tax,
"std" => "Choose a category"),
and I can't get the taxonomies list:
$args=array( 'name' => 'Catalogs');
$output = 'names'; // or objects
$taxonomies = get_taxonomies($args,$output);
$wp_tax = array();
foreach ($taxonomies as $category_list ) {
$wp_tax[$category_list->ID] = $category_list->name;
}
array_unshift($wp_tax, "Choose a category");
What's wrong? I can't get it to work :(
Solution
A taxonomy is a group of terms. I think you registered a taxonomy Catalogs, and now you want to list all terms in this taxonomy. You do that with the function get_terms()
, not get_taxonomies()
.
So your $wp_tax
array should be filled like this:
$wp_tax = array(-1 => 'Choose a category');
$catalog_terms = get_terms('Catalogs');
if ($catalog_terms) {
foreach ($catalog_terms as $catalog_term) {
$wp_tax[$catalog_term->term_id] = $catalog_term->name;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange