The function below is inside a single post loop.
I have an array of IDs in a custom field. Such IDs represent related posts. I'm using Advanced Custom Fields (ACV) plugin and I manage the custom metas with it. In this case, to get the related_content meta, I can use the ACV function get_field('related_content') and grab post data from each retrieved post in the related_content field. According to the number of related posts retrieved, I want to display the results slightly different (using larger thumbnails or no thumbnails at all, etc.). So I came up with the php you see below. However, I'm not sure if this is efficiently optimized or can be written better. Would you mind checking it out?
I am aware this is not strictly a Wordpress question but also PHP/Database related, but since it's related to wordpress objects, database and functions I thought it would be appropriate to post here. Maybe there's a better way to query WP for what I want... thank you
echo '<ul>';
foreach( get_field('related_content') as $post_object) :
$post_type = get_post_type_object(get_post_type($post_object->ID));
for ($i = 0; $i <count($post_object); $i++ ) {
if ($i == 0 && $i < 4 ) {
printf('<li class="large"><a target="_blank" hre= "'.get_permalink($post_object->ID).'">'.get_the_post_thumbnail($post_object->ID, 'small').'</li><span class="thumb-cat">'.$post_type->labels->singular_name.'</span><span class="thumb-title">'.get_the_title($post_object->ID).'</span></a></li>');
}
elseif ($i > 3 && $i < 5 ) {
printf('<li class="medium"><a target="_blank" href="'.get_permalink($post_object->ID).'">'.get_the_post_thumbnail($post_object->ID, 'smallest').'</li><span class="thumb-cat">'.$post_type->labels->singular_name.'</span><span class="thumb-title">'.get_the_title($post_object->ID).'</span></a></li>');
}
elseif ($i > 6 ) {
printf('<li class="small"><a target="_blank" href="'.get_permalink($post_object->ID).'"><span class="thumb-cat">'.$post_type->labels->singular_name.'</span><span class="thumb-title">'.get_the_title($post_object->ID).'</span></a></li>');
}
}
endforeach;
echo'</ul>';