Ok so I have a custom post type of “Products” and a custom taxonomy of “Types”. Now in the types taxonomy I have dozens of categories and sub categories assigned to custom post types.
Custom Post Type Item: Blue Car
Categories assigned to car: Honda > Accord > LX
So what I am trying to achieve is when I go to a specific custom post types single.php like “Blue Car” I want to see all of the cateogies assigned to “Blue Car” listed as so:
Categories: Honda, Accord, LX
I have tried using get_terms_by get_terms and wp_list_categories but it doesn’t seem to be working out or im not using some of them correctly.
Heres what I have feel free to let me know if there is a better way or another way to do this:
<ul>
<?php
//get the current term
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
//set the args for wp_list_categories
$args = array(
'child_of' => $current_term->term_id,
'taxonomy' => 'types',
'hide_empty' => 1,
'order' => 'ASC',
'show_count' => 1,
'hierarchical' => true,
'depth' => 1,
'title_li' => ''
);
wp_list_categories( $args );
?>
</ul>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
SIDENOTE: This function can be used even if have posts belongs to one taxonomy only. You don’t need to have a post that belongs to two or more taxonomies within the same hierarchy. I have made this function very flexible
It is always a problem for me to display a category list or term list for a post if the following conditions occur
- Having a post having terms that belongs to two different taxonomies
- Using one template to display posts, and you have posts that have terms belonging to more that one taxonomy
You come to a situation where you need to duplicate functions, one for each taxonomy. For instance, a post belongs to terms in two taxonomies, mytax1 and mytax2. To display the term list, you have to do
echo get_the_term_list( $post->ID, 'mytax1' ); echo get_the_term_list( $post->ID, 'mytax2' );
This become a mess when the next post belongs to the default taxonomy category
I went and wrote a function that combines all in one, one function to display all terms of all taxonomies belonging to a post, and this function can be used to display any taxonomy of the build in taxonomies (except post_format)
Here is how the function works:
1.) This function makes use of get_the_term_list() to display the term list, so this function uses the same functionality as get_the_term_list()
2.) Must be used inside the loop. This function will not work outside the loop
3.) The post ID and taxonomy name are passed by default to the get_the_term_list() function
4.) Here is the list of the arguments
before (string)(optional) Leading text
Default: empty string
sep (string)(optional) String to separate tags
Default: empty string
after (string)(optional) Trailing text
Default: empty string
display_tax_name (bool)(optional) Should the taxonomy name be displayed in front of the list.
Default: false
taxonomy_sep (string)(optional) Text used to separate the taxonomy name from the term list
Default:
': 'multi_tax_sep (string)(optional) If the post have terms that belongs to more than one taxonomy, the text used to separate the two or more term lists
Default:
</br>hierarchical (bool)(optional) Should the list display hierarchical taxonomies (like categories ) or non-hierarchical taxonomies (like post tags)
Default: true
5.) The arguments can be passed as an array or as a string to the function
6.) Needs PHP 5.4+
This will work (string)
'display_tax_name=' . true .'&hierarchical=' . false . '&taxonomy_sep=' . html_entity_decode( '» ' )
And this will work
$args = [
'display_tax_name' => true,
'hierarchical' => false,
'taxonomy_sep' => html_entity_decode( '» ' )
]
The function
function get_taxonomies_terms_links( $args = '' ){
global $post;
$defaults = [
'before' => '',
'sep' => '',
'after' => '',
'display_tax_name' => false,
'taxonomy_sep' => ': ',
'multi_tax_sep' => '</br>',
'hierarchical' => true
];
$args = wp_parse_args( $args, $defaults );
$post_type = $post->post_type;
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$returned_list = [];
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if( $args['hierarchical'] == $taxonomy->hierarchical && has_term( '', $taxonomy_slug ) && 'post_format' != $taxonomy_slug ) {
$term_list = get_the_term_list( $post->ID, $taxonomy_slug, $args['before'], $args['sep'], $args['after'] );
if( true == $args['display_tax_name'] ){
$returned_list[] = strtoupper($taxonomy_slug) . $args['taxonomy_sep'] . $term_list;
}else{
$returned_list[] = $term_list;
}
}
}
if( $returned_list ) {
$count = count($returned_list);
if( 1 === $count ) {
return implode( '', $returned_list );
}else{
$multi_list = [];
foreach ( $returned_list as $key=>$value ) {
if (array_key_exists($key + 1, $returned_list)) {
$multi_list[] = $value . $args['multi_tax_sep'];
}else{
$multi_list[] = $value;
}
}
return implode( '', $multi_list );
}
}
}
Example of use in a template to display the term list. You can either make use of the normal reading characters or HTML entities in conjunction with html_entity_decode() and the character chart
Arguments as string
<span class="cat-links">
<?php echo get_taxonomies_terms_links('sep=, &display_tax_name=' . true .'&taxonomy_sep=' . html_entity_decode( '» ' ) ); ?>
</span>
Arguments as an array
<span class="cat-links">
<?php
echo get_taxonomies_terms_links(
[
'sep' => ', ',
'display_tax_name' => true,
'taxonomy_sep' => html_entity_decode( '» ' )
],
);
?>
</span>
Method 2
The functions you have tried are for terms by themselves, just existing in site.
What you are looking for is get_the_terms(), which retrieves terms of specific taxonomy, assigned to specific post.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0