简体   繁体   中英

Return first image in gallery array of Pods Framework in WordPress

I have a mutidimensional array called 'gallery' in Pods that only contains photographs. I only want to return the first image of the array in my WordPress page, however all of the pics in the array are being displayed. Here's the code I'm currently using:

<?php foreach($vehiclepods->field('gallery', true) as $picture) { ?> <img src="<?php echo $picture['guid'];?>" alt="image" /></a> <?php } ?>

What am I doing wrong and how can I just get the first image in the array to show up?

screenshot of output

Full PasteBin of the index file is here.

Well your output for images is wrapped in a foreach. So your going to get every image if you only want the first one use

$gallery = $vehiclepods->field('gallery', true);

<img src="<?php echo $gallery[0]['guid'];?>" alt="image" />

This way gallery is an array and you are getting the first image src in the array. with [0][guid]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM