php - 如何将 "else"添加到 "foreach"?

标签 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>';
    }
?>

最佳答案

您可以尝试类似的方法:

<?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
    }
?>

关于php - 如何将 "else"添加到 "foreach"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432802/

相关文章:

javascript - React 中的 CSRF 字段 (JSX)

PHP (Laravel) 依赖注入(inject)基础

php - Chrome 删除 'to' 之后的空格

PHP foreach循环插入数据库

PHP foreach 循环与序列号同时显示 MySQL 结果

PHP:如何检查客户端是否是本地的?

php - 日期格式在 PHP 中使用 DateFmt 在法语中返回错误的年份

javascript - Angular 重复-重复一次

php - Fancybox:使用背景图片

php - 如何将子菜单下拉列表添加到我当前的 wp 主题顶部菜单?