You should use wp_trim_words() and inside place the title for the post:
wp_trim_words(get_the_title(), 5, '...')
In your case it should be something like this:
if ( has_post_thumbnail() ) {
$string .= '<p><a href="' . get_the_permalink() .'" style="color:#FFF;" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 100, 50) ) . (wp_trim_words(get_the_title(), 5, '...')) .'</a><br></p>';
} else {
// if no featured image is found
$string .= '<li><a href="' . get_the_permalink() .'" style="color:#FFF;" rel="bookmark">' . (wp_trim_words(get_the_title(), 5, '...')) .'</a></li>';
}
For more details on how wp_trim_words()
works you could visit the official documentation: https://developer.wordpress.org/reference/functions/wp_trim_words/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…