'get_the_terms orderby and separator on CPT
I had a problem this morning but finally resolved it. Thing is, now that it is ok, it leaves me with another problem.
Long story short : I created a CPT, added a hierarchical taxonomy to use for my categories, and then created a Buddypress tab to show the posts my users publish in the CPT. Though I finally managed to display the posts with the correct template call, I'm now facing a Categories issue.
Here is the code I'm using at the moment to get the post categories
<?php
$terms = get_the_terms( $post->ID , 'cptgaleriecat' );
$sep = ' • ';
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'cptgaleriecat' );
if( is_wp_error( $term_link ) )
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
echo $sep;
}
?>
It works great. I finally get the link to my categories archives. But for some reason, if I use "wp_get_post_terms" to add an array to order my categories by "term_order" so the smaller displays first, it doesn't work. It won't accept "wp_get_post_terms" to retrieve the categories. Is there a way to order the terms with "get_the_terms" or how can I make "wp_get_post_terms" to work?
And the other thing is, for now the separator shows even after the last term. How do I get rid of it?
Thank you for your help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|