1

How can i add different text for each of result in foreach loop

For example

image 1 , "sample text 1", image 2, "sample text2

 <?php 
    $id = 51;
    $count = 0;
    $is = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&order_by=ASC");
    foreach($is as $i) {
      $url = wp_get_attachment_image_src($i->ID, 'full');?>
        <img src="<?php echo $url[0]; ?>">

     <?php 
    }
 ?>
1

1 Answer 1

-1

You can use the wp_get_attachment_metadata() function which returns an array of data.

I would add it to your code

<?php 
$id = 51;
$count = 0;
$is = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&order_by=ASC");
foreach($is as $i) {
  $url = wp_get_attachment_image_src($i->ID, 'full');
  $attachementMeta = wp_get_attachment_metadata($i->ID);
?>
    <img src="<?php echo $url[0]; ?>" alt="<?php echo $attachementMeta['image_meta']['caption']; ?>">

 <?php 
} 
?>

Read more about the wp_get_attachment_metadata() here

Hope this helps.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.