WordPress query post get Image unless blank

Hi I am creating a wordpress custom page theme, I have included the code. Is there a way to dynamically add the list items. In the code I grab each image related to its post with screenshot1 screenshot2 so on… This all currently works. My problem is at the current moment if I upload 2 screen shots 3 list items will show up and the third will just be blank. So how could I dynamically add them based on the number of images in the post?

query_posts('cat=3'); /*--Query to grab Projects categorie--*/

while (have_posts()) : the_post();  /* --- loop through the posts in that categorie */

<ul>

<?php
$screen1 = get_post_meta($post->ID, 'screenshot1', true);
$screen2 = get_post_meta($post->ID, 'screenshot2', true);
$screen3 = get_post_meta($post->ID, 'screenshot3', true);
echo "<li>" . wp_get_attachment_image($screen1, 'large') . "</li>"; 
echo  "<li>" . wp_get_attachment_image($screen2, 'large') . "</li>";
echo  "<li>" . wp_get_attachment_image($screen3, 'large') . "</li>";
?>

</ul>

Related posts

Leave a Reply

1 comment

  1. Loop through the possible meta values and only output it if it’s not empty.

    Something like:

    <ul>
    <?php
    $i = 1;
    while ($screen = get_post_meta($post->ID, 'screenshot'.$i , true)) {
        echo "<li>" . wp_get_attachment_image($screen, 'large') . "</li>"
        $i++; 
    }
    ?>
    </ul>