遍历数组或交叉数组

WSP网站管理员

我对 php 相当陌生,并试图echo "<p>" .get_the_tags($author_post). "</p>";在循环中回显与其列表相关的标签。

有人告诉我“你需要遍历数组”和“这会返回一个标签数组。如果你想回显它,你必须穿过这个数组。” 但没有被告知如何做到这一点。我不确定如何继续。

这是完整的代码。

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()回报是array

试试这个:

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

echo '</li>';

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章