0

I am fairly new to php and trying to get echo "<p>" .get_the_tags($author_post). "</p>"; to echo out the tags associated with the post its listing in the loop.

I was told to "you need to traverse the array" and "This returns an array of tags. Then you have to cross the array if you want to echo it out." but was not told how to accomplish this. I am unsure how to proceed.

Here is the full code.

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

Thanks for any help you can provide

1 Answer 1

1

I guess your get_the_tags() return an array

try this:

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

echo '</li>';

BTW , this syntax is a bit old...

Sign up to request clarification or add additional context in comments.

1 Comment

After more digging and hitting my head I was able to come up with $tags = get_the_tags($author_post); echo '<div class="righttext">- Tags: '; foreach ( (array) $tags as $tag ) { echo $tag->name. ', '; } echo '</div>'; That did what I needed, somewhat, because now there is an extra (comma) at the end of the list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.