i don't know how to add else
to foreach
!
here is my code:
<?php $terms = get_the_terms( $post->ID , 'actor' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'actor' );
if( is_wp_error( $term_link ) )
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
}
?>
You could try something similar:
<?php
$terms = get_the_terms( $post->ID , 'actor' );
if ($terms) {
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'actor' );
if ( is_wp_error( $term_link ) )
continue;
echo '<a href="' . $term_link . '">' . $term->name . '</a>';
}
} else {
// do the work for else
}
?>
foreach
is a loop. else
is part of a conditional statement. It looks like you just want a conditional inside your loop. This would just be a standard conditional.
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
I assume you want to break
the loop in that case.