Agh*_*ree -3 php wordpress foreach
我不知道如何添加else到foreach!
这是我的代码:
<?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>';
}
?>
Run Code Online (Sandbox Code Playgroud)
您可以尝试类似的方法:
<?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
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |