I use the woothemes canvas theme, and need to be able to add a function that will list all the terms belonging to a custom taxonomy via a dropdown.
Here is a couple of examples that currently work within the theme to pull in categories and Post-types.
//Access the WordPress Categories via an Array
$woo_categories = array();
$woo_categories_obj = get_categories('hide_empty=0');
foreach ($woo_categories_obj as $woo_cat) {
$woo_categories[$woo_cat->cat_ID] = $woo_cat->cat_name;}
$categories_tmp = array_unshift($woo_categories, "All Categories:");
//Access the WordPress Post types via an Array
$cpt_args = array( 'public' => true,'_builtin' => false);
$out = 'names';
$operator = 'and';
$woo_posttypes = array();
$woo_posttypes_obj = get_post_types($cpt_args,$out,$operator);
foreach ($woo_posttypes_obj as $woo_posttype) {
$woo_posttypes[] = $woo_posttype; }
$woo_posttypes_tmp = array_unshift($woo_posttypes, "Select a post type:");
What I need:
I have a custom post-type 'photogallery' that has a custom taxonomy 'gallerycategories'. I need a dropdown just like the above examples that will list out the terms belonging to the gallerycategories.
Thank you
Nilesh shiragave answers:
Hi
Try this code
<?php $args = array(
'type' => 'photogallery',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'gallerycategories'
);
$categories = get_categories( $args );
?>
shawn comments:
Yes that works, but not in the format that I am needing.
I provided 2 functions that do similar functions that work properly. Basically I think it's simply asking to return an array of terms that can be used to populate a dropdown in my theme admin.
If you can format it like the first 2, then I can try it out. I've tried every combination I can think of with no luck so far
Nilesh shiragave comments:
So you want a terms dropdown for the "gallerycategories" taxonomy?
shawn comments:
yes.
The theme admin takes care of the dropdown part, I just need to populate the dropdown.
The 2 code examples above do this for me perfectly, which is why I included them as examples.
I simply need to modify one of the 2 above snippets to retrieve terms for gallerycategories.
I can then add that function name to my dropdown options box.
shawn comments:
This probably won't mean anything to you, but here is the code in my theme options table for pulling in the post-types, it is populated by the function in my first post.
$options[] = array( "name" => "Custom Post Type",
"desc" => "Select a custom post-type to display",
"id" => $shortname."_panel_1_post_type",
"std" => "",
"type" => "select",
"class" => "panel_1_post_type",
"options" => $woo_posttypes);
I think now you can see I just need to replace
"options" => $woo_posttypes);
with the function that you provide me to populate the drop down
Nilesh shiragave comments:
try this
$args = array(
'type' => 'photogallery',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'gallerycategories'
);
$categories = get_categories( $args );
$woo_terms=array();
foreach ($categories as $woo_cat) {
$woo_terms[$woo_cat->cat_ID] = $woo_cat->cat_name;
}
$categories_term = array_unshift($woo_terms, "All Categories:");
shawn comments:
PHP Catchable fatal error: Object of class stdClass could not be converted to string in ...
Nilesh shiragave comments:
Try this code
$args = array(
'type' => 'photogallery',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'gallerycategories'
);
$categories = get_categories( $args );
$woo_terms=array();
foreach ($categories as $woo_cat) {
$woo_terms[$woo_cat->cat_ID] = $woo_cat->cat_name;
}
For options panel Use $woo_terms like
"options" => $woo_terms);
shawn comments:
Perfect again.
Thank you!
Probably have a few more questions posted tomorrow as it's 1AM here now.
Nilesh shiragave comments:
Great :)
Bob answers:
Use an array like the first category one above you posted but instead of using
get_categories
use
get_the_term_list
in your case it would be something like this to output a list of taxonomy's based on post ID
<?php get_the_term_list( $post->ID, 'gallerycategories', '<li>gallerycategories: ', ', ','</li>' ); ?>
I'm sorry I can't help much further, I am not familiar with Wootheme's code.
idt answers:
Can you try this?
$woo_terms = array();
$woo_terms_obj = get_terms('gallerycategories', 'hide_empty=0');
foreach ($woo_terms_obj as $woo_term) {
$woo_terms[] = $woo_term;
}
$woo_terms_tmp = array_unshift($woo_terms, "Select a taxonomy:");