Reputation: 1077
I am really desperate for help here, been trying do this since yesterday and still no luck, so here is what I want to achieve
Collection = custom post type
Material = custom taxonomy assigned to "collection"
Body = custom taxonomy assigned to "collection"
Color = custom taxonomy assigned to "collection"
The structure will be something like this
- Material
- Mat1
- Mat2
- Body
- Body1
- Body2
- Color
- Color1
- Color2
I got this code but it's not working at all
$post_type = 'collection';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );
foreach( $taxonomies as $taxonomy ){
echo $taxonomy->name;
$terms = get_terms( $taxonomy );
foreach( $terms as $term ){
echo '<li>';
echo $term->name;
echo '</li>';
}
}
Upvotes: 2
Views: 8197
Reputation: 13840
If you look at the documentation for get_object_taxonomies()
you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies()
and get_terms()
.
Also with the get_terms()
function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args
array.
$taxonomies = get_object_taxonomies( 'collection', 'objects' );
foreach( $taxonomies as $taxonomy ){
echo $taxonomy->name;
$terms = get_terms(array(
'taxonomy' => $taxonomy->name,
'hide_empty' => false,
));
foreach( $terms as $term ){
echo "<li>{$term->name}</li>";
}
}
Upvotes: 7