0

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>';
1
  • 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
0

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) :
        $count++;
        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>');
            }
            endforeach; 
            echo'</ul>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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