0
votes

Using wordpress get_post_meta $key. I was able to output attachment image in single.php using Function Reference/wp get attachment image src for single attachment id:

<?php 
$attachment_id = get_post_meta($post->ID, 'attachment-id', true); // attachment ID

$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
if( $image_attributes ) {
?> 
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
<?php } ?>

Since I am coding image gallery, I intend to use php foreach function to html output multiple images html code replacing the single post_meta $key with multiple ids array like (55,32,34). I need help with the code required to restructure the output due to my lack of PHP knowledge.

1

1 Answers

0
votes

You need to know which IDs you will look for, considering you have an array of ids :

$attachmentIds = [55,32,34];

You can then loop trough them with the foreach function and perform the same thing as what you did :

foreach ($attachmentIds as $id) {
    $image_attributes = wp_get_attachment_image_src( $id );
    if( $image_attributes ) {
        echo '<img src="'. $image_attributes[0] .'" width="' . $image_attributes[1] . '" height="'. $image_attributes[2] .'">';
    }
}