T
T
there42018-06-09 13:19:19
WordPress
there4, 2018-06-09 13:19:19

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>';
  }
}

Can someone tell me how to create a check for the_field in this loop for the absence of data in it, and depending on this, output either
echo 'photo_none.jpg';
the_field( 'img-tax', $cur_term );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2018-06-09
@there4

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 question

Ask a Question

731 491 924 answers to any question