Question

I'm using advanced custom fields in wordpress. I made this custom post type and attached it 10 image upload fields through ACF.

I can display one image at a time by doing this:

<?

$image = get_field('img_nr1'); 

if( !empty($image_nr1) ): ?>

<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>

But what doing this for all 10 images seems to much. I was wondering if I can assing the 10 images with in a array and display them somehow.

Would the array look something like this:

$image = array (
        'image1' => 'img_nr1',
        'image2' => 'img_nr2',
        'image3' => 'img_nr3',
        'image4' => 'img_nr4',
        'image5' => 'img_nr5',
        'image6' => 'img_nr6',
        'image7' => 'img_nr7',
        'image8' => 'img_nr8',
        'image9' => 'img_nr9',
        'image10' => 'img_nr10'
)

the array probably is inaccurate, but if so what would it look like and and how could I display it afterwards?

Was it helpful?

Solution

PHP can count, you know :)

<?php
for ( $i = 1; $i <= 10; $i++ ):
    $image = get_field( 'img_nr' . $i ); 

    if ( ! empty( $image ) ): ?>
        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    <?php endif;
endfor;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top