Answer the question
In order to leave comments, you need to log in
How to display a field only when it is filled?
On a WordPress site, on the post page, to display category names without a link (custom taxonomy category 'genres'), the following code is used:
<?php
$array = array();
$cur_terms = get_the_terms( $post->ID, 'genres');
foreach( $cur_terms as $cur_term ){
$array[] = $cur_term->name;
}
$comma_separated = implode(", ", $array);
echo '<div>Жанры:' . ' ' . $comma_separated . '</div>'; // term1, term2, term3
?>
if
) correctly.
Answer the question
In order to leave comments, you need to log in
$cur_terms = get_the_terms( $post->ID, 'genres' );
if( $cur_terms && ! is_wp_error( $cur_terms ) ) {
$array = array();
foreach( $cur_terms as $cur_term ) {
$array[] = $cur_term->name;
}
$comma_separated = implode( ", ", $array );
echo '<div>Жанры: ' . esc_attr( $comma_separated ) . '</div>'; // term1, term2, term3
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question