Behseini
Behseini

Reputation: 6320

PHP Echo Also Outoug images alt Attribute

Can you please take a look at this snippet and let me know why I am outputting the alt attribute in

 echo '<img src="'.wp_get_attachment_link($image, 'large').'" alt="Projects" />';

in following snippet:

$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) {
  echo '<div class="col-xs-6 col-md-3">';
    echo '<div class="thumbnail">';
      echo '<img src="'.wp_get_attachment_link($image, 'large').'" alt="Projects" />';
    echo '</div>';
  echo '</div>';
}

here is what happening

enter image description here

Upvotes: 1

Views: 1067

Answers (3)

Vinie
Vinie

Reputation: 2993

error in this line

echo '<img src="'.wp_get_attachment_link($image, 'large').'" alt="Projects" />';

Your error are due to ' in 'large' change to below

$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) {?>
 <div class="col-xs-6 col-md-3">
  <div class="thumbnail">
  <img src="<?=wp_get_attachment_link($image, 'large')?>" alt="Projects" />
  </div>
</div>
<?php }?>

Upvotes: 0

Abhinav
Abhinav

Reputation: 408

TRY LIKE THIS

echo "<img src='".wp_get_attachment_image_src($image,'large')."' alt='Projects'/>";

Upvotes: 0

AlmasK89
AlmasK89

Reputation: 1340

wp_get_attachment_link returns an html link https://codex.wordpress.org/Function_Reference/wp_get_attachment_link
Use wp_get_attachment_image_src instead https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

$images = get_post_meta($post->ID, 'vdw_gallery_id', true);
foreach ($images as $image) { ?>
    <div class="col-xs-6 col-md-3">
        <div class="thumbnail">
            <?php 
            $img = wp_get_attachment_image_src($image,'large');
            if ($img && isset($img[0])):
            ?>
            <img src="<?php echo $img[0];?>" alt="Projects" />
            <?php endif;?>
        </div>
    </div>
<?php }?>

Upvotes: 5

Related Questions