I have a simple function that displays values followed by a comma.
if ( $hasCoAuthors ) :
foreach ( $co_authors as $co_author ) :
$coAuthorDisplayName = $co_author->display_name;
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>, ';
echo $coAuthor;
endforeach;
endif;
How to get the last value to remove the comma from it?
I added this function:
function endKey( $array ) {
end( $array );
return key( $array );
}
Then I added this in my function:
if ( endKey( $co_authors ) ) :
$coAuthor = '<span class="co-author">' . $coAuthorDisplayName . '</span>';
endif;
But it doesn't work, it removes commas from all values.