Hello,
I have a custom taxonomy (<em>Product Categories</em>) setup for a custom post type (<em>Products</em>) and wish to display the terms of each post individually.
If the PRODUCT is Brand 1 and Type 2 I can display them separately as eg; "This Products Brand is 'Brand 1' and it's type is 'Type 1'"
I can use wp_get_post_terms but not sure how to get child terms within that and separate them. Any Ideas?
My Taxonomy Setup;
<strong>Product Categories</strong>
<strong>Brand</strong>
Brand 1
Brand 2
<strong>Type</strong>
Type 1
Type 2
Many thanks in advance!
Baki Goxhaj answers:
Here you go:
$terms = get_the_terms( $post->ID, 'brand' );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) :
$out[] = '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a>';
endforeach;
echo join( ', ', $out );
}
Replicate for "Type" taxonomy.
floy comments:
Using that throws me an error.
Catchable fatal error: Object of class WP_Error could not be converted to string in
floy comments:
Is there someway to use <strong>get_the_terms( $id, $taxonomy )</strong> and refine the output to the child terms of a specific parent?
Baki Goxhaj comments:
Yes, there is a way. The object have a "parent" attribute as part of the data, thus it makes it possible.
Did you figure out the error? It seems fine on my end.
I will work on it now so you have only the children shown up as you describe.
floy comments:
Still getting the error. Not sure why.
Baki Goxhaj comments:
I figured it. Will get with the updated version in a while. Hold on.
Baki Goxhaj comments:
Here you go:
This works for sure because I got the result locally. Please edit those items I've commented. If you don't know how to find the term ID, read this: http://wplancer.com/how-to-find-a-wordpress-category-id/
<?php
$taxonomy = 'category'; // EDIT - put your taxonomy slug. I guess it is "product_category".
$terms = get_the_terms( $post->ID, $taxonomy );
$brand_children = '';
$type_children = '';
foreach ( $terms as $term ) :
if( $term->parent == 1 ) { //EDIT - Put the right ID here.
$brand_children .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li>';
} elseif( $term->parent == 3 ) { //EDIT - Put the right ID here.
$type_children .= '<li><a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a></li>';
}
endforeach;
?>
<h3>Brand</h3>
<ul>
<?php echo $brand_children; ?>
</ul>
<h3>Type</h3>
<ul>
<?php echo $type_children; ?>
</ul>
floy comments:
That is EXACTLY what I was after! Your a genius!
I came up with something super similar but just couldn't get the foreach loop to work.
Thanks!
Baki Goxhaj comments:
<blockquote>Your a genius!</blockquote>
Thanks very much for that - it really pays as good as the money. I just focused and got your question right the second time :)
Albert Shala answers:
Try this:
<?php
$termID = 10;
$taxonomyName = "products";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
More info here: http://codex.wordpress.org/Function_Reference/get_term_children
floy comments:
That just outputs all the child terms of the custom tax.
I'm after the child terms of a specific parent term from the custom tax from a particular post.
<strong>Product Categories</strong>(CUSTOM TAXONOMY)
<strong>Brand</strong>(Term)
Brand 1(Child Term)
Brand 2(Child Term)
<strong>Type</strong>(Term)
Type 1(Child Term)
Type 2(Child Term)
If the PRODUCT is Brand 1 and Type 2 I can display them separately as eg; "This Products Brand is 'Brand 1' and it's type is 'Type 1'"
Albert Shala comments:
Ok give this a try:
$taxonomyName = "Product Categories";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));
echo '<ul>';
foreach ($parent_terms as $pterm) {
//Show the child terms
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
foreach ($terms as $term) {
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
floy comments:
Still returning me with ALL the terms instead of the Terms related to the specific post.
Would have to use the <strong>wp_get_post_terms</strong> function im guessing, but im not too sure.