遍历数组或跨数组

Traverse an array or cross an array

我是 php 的新手,正在尝试让 echo "<p>" .get_the_tags($author_post). "</p>"; 回显与循环中的 post 相关的标签。

有人告诉我 "you need to traverse the array" 和 "This returns an array of tags. Then you have to cross the array if you want to echo it out." 但没有被告知如何实现这一点。我不确定如何进行。

这是完整的代码。

if ($author_posts) {
    echo '<ul>';
    $i = 0;
    foreach ($author_posts as $author_post) {
        /* excluded categories   */
        if (has_category(explode(',', $atts['exclude']), $author_post->ID)) :
            continue;
        endif;
        $postdate = date_i18n( get_option( 'date_format' ), strtotime($author_post->post_date)).' - ';  
        echo '<li>';
        echo ($atts['postdate'] ? $postdate : ''). '<a href="' . get_permalink( $author_post->ID ) . '">'.$author_post->post_title.'</a>';
        $categories = get_the_category( $author_post->ID );
        $list_cats =null;
        foreach ($categories as $cat) :
            $list_cats .= $cat->name.", ";
        endforeach;
        $list_cats = substr($list_cats, 0, -2);
        echo "<p>" .get_the_tags($author_post). "</p>";
        echo '</li>';
        $i++;
        if ($atts['postsperauthor'] > -1) :
            if ($i >= $atts['postsperauthor']) :
                break;
            endif;
        endif;
    }
}

感谢您提供的任何帮助

我猜你的 get_the_tags() return 一个 array

试试这个:

$tags = get_the_tags($author_post);
$tagNames = [];
foreach ( $tags as $tag ) {
    $tagNames[] = $tag->name
}
echo implode(',',$tagNames)
echo '</div>';

echo '</li>';

顺便说一句,这个语法有点老了...