WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>');
share|improve this question
So what exactly is your question? How to improve your code? I'm not sure that fits the requirements on this site, anything you can do to improve the question? – t31os Dec 8 '11 at 13:39
up vote 0 down vote accepted

I answer to myself, since this code is working better (previous had an issue) and also I removed a useless "for":

echo '<ul class="related-content">';
$count = 0;
foreach(get_field('related_content') as $post_object) :
        if ($count > 0 && $count < 4 ) { 
            printf('<li class="large"><a target="_blank" title="'.get_the_title($post_object->ID).'" href="'.get_permalink($post_object->ID).'"><span style="display: block" title="'.get_the_title($post_object->ID).'">'.get_the_post_thumbnail($post_object->ID, 'small').'</span><span class="thumb-title">'.get_the_title($post_object->ID).'</span></a></li>');
            elseif ($count > 3 && $count < 6 ) { 
                    printf('<li class="medium"><a target="_blank" href="'.get_permalink($post_object->ID).'">'.get_the_post_thumbnail($post_object->ID, 'smallest').'<span class="thumb-title">'.get_the_title($post_object->ID).'</span></a></li>');
            elseif ($count > 6 ){ 
                    printf('<li class="small"><a target="_blank" href="'.get_permalink($post_object->ID).'"><span class="thumb-title">'.get_the_title($post_object->ID).'</span>'. get_field('title_subtitle').'</a></li>');
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.