I’m trying to get a page’s featured image alt and echo it as paragraph text but my code doesn’t seem to be working.
I’m currently able to echo the image and it’s working perfectly.
Here’s 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>
Check if you get correct thumbnail id.
For me this code works perfect:
Here’s a solution:
Or you can use your code, but instead of echoing
$alt
directly you need to echo$alt->post_excerpt
.If you want to get the alt text of your featured image, you can get it with this code…
So if you want to place it in a P tag, this will work…
Hope it helps you, or someone at least.