1

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?

1 Answer 1

2

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;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly! Thank you so much! Yes, I know, but I'm not that comfortable with PHP yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.