Get Wordpress Recommended Image "alt"

I am trying to get an image with the image of the alt page and repeat it as paragraph text, but my code does not seem to work.

Currently I can echo the image and it works great.

Here is the code I'm using:

    <?php
    get_header(); ?>
      </div>
    <?php /* The loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div class="header-image">

    <?php echo get_the_post_thumbnail($page->ID, 'full'); ?> 
    
    <?php $alt = get_post_meta( $attachment_img->ID, '_wp_attachment_image_alt', true ); ?>
    
    <p><?php echo $alt; ?></p>
    
    </div>
+4
source share
2 answers

Here's the solution :

  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
  }

Or you can use your code, but instead of an echo, $altyou will need an echo $alt->post_excerpt.

+4
source

Verify the sketch icon is correct.
For me, this code works fine:

$thumbnail_id = get_post_thumbnail_id( $post->ID );
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
+9
source

All Articles