Answer the question
In order to leave comments, you need to log in
How to set an if condition in a foreach loop?
I'm trying to display the image for the taxonomy, and, in case of its absence, output photo_none.
Now it outputs "value=img-tax+photo_none.jpg" (img src="/wp-content/uploads/tax1125.jpgphoto_none.jpg"):
function tax_img() {
$cur_terms = get_the_terms( $post->ID, 'my-tax' );
foreach( $cur_terms as $cur_term ){
echo '<span><img src="';
the_field( 'img-tax', $cur_term );
echo 'photo_none.jpg';
echo '"/>';
echo '<a href="'. get_term_link( (int)$cur_term->term_id, $cur_term->taxonomy ) .'">'. $cur_term->name .'</a></span><br>';
}
}
echo 'photo_none.jpg';
the_field( 'img-tax', $cur_term );
Answer the question
In order to leave comments, you need to log in
if($img = get_field( 'img-tax', $cur_term )) {
echo $img;
} else {
echo 'photo_none.jpg';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question