my goal is to display related articles below a post (check if available, then loop through the ones that are there). It works fine if I have 3 or 6 setup but if I have only 5, it displays automatically the original post as the sixth related article.
echo "<div class='related-posts'>";
$related_articles = get_field('related_articles', false, false);
$id = $related_articles[0];
if($id) {
echo "<h5 class='related-posts_h'>Ãhnliche Artikel, die du lesen musst</h5>";
echo "<ul class='related-posts_list clearfix'>";
for ($i=1; $i <= 6; $i++) {
$id = $related_articles[$i-1];
if ( has_post_thumbnail($id) ) {
echo "<li class='related-posts_item'>";
echo "<figure class='featured-thumbnail thumbnail large'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)." title='".get_the_title($id)."'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
echo "<span class='zoom-icon'></span>";
echo "</a>";
echo "</figure>";
} else {
"<figure class='thumbnail featured-thumbnail'>";
echo "<div class='hider-page'></div>";
echo "<a href='".get_permalink($id)."' title='".get_the_title($id)."'>";
echo "<img src='".get_template_directory_uri()."/images/empty_thumb.gif' alt='".get_the_title($id)."' />";
echo "</a>";
echo "</figure>";
}
echo "<h6><a href='".get_permalink($id)."'>";
echo get_the_title($id);
echo "</a>";
echo "</h6>";
echo "</li>";
}
echo "</ul>";
echo "</div><!-- .related-posts -->";
Try this.. check after the for loop whether the related Post ID is not equals to the original post ID then only display the related Posts
that is
I Hope you are in the details or single.php page so you will get the ID of original post by
get_the_ID()