I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values that the taxonomy contains. How can I do to show the correct value to each entry? I don't know about programming. I appreciate you being clear in the answer. Thank you
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,)));
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
I have a function to obtain specific taxonomies in each entry. The problem is, for each entry, it shows all the values that the taxonomy contains. How can I do to show the correct value to each entry? I don't know about programming. I appreciate you being clear in the answer. Thank you
$terms = get_terms( ( array('taxonomy' => 'dormitorio', 'hide_empty' => false,)));
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
Share
Improve this question
asked Feb 6, 2020 at 8:41
MaciMaci
11 bronze badge
1
- I have it inside functions.php How can I use get_the_terms()? I changed it and I 've got differents error. – Maci Commented Feb 6, 2020 at 10:52
2 Answers
Reset to default 0Please specify where are you using the code above, if in a template file or in a function.
Assuming the code above is for a single post and that you're trying to retrieve the dormitorio
terms associated with that post you should use get_the_terms()
instead of get_terms()
.
get_terms() Retrieves the terms in a given taxonomy or list of taxonomies
whilst
get_the_terms() Retrieves the terms of the taxonomy that are attached to the post
I've found the solution:
$terms = get_the_terms( get_the_ID(), 'dormitorio' );
if ( isset( $terms ) && '' !== $terms ) {
foreach( $terms as $term ) {
echo '<div class="tax dormitorio ' . $term->slug . '">';
echo $term->name;
echo '</div>';
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770572a4592733.html
评论列表(0条)