Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Wordpress Featured Image "alt"

Tags:

php

wordpress

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>
like image 424
babusi Avatar asked Oct 09 '13 09:10

babusi


1 Answers

Check if you get correct thumbnail id.
For me this code works perfect:

$thumbnail_id = get_post_thumbnail_id( $post->ID );
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
like image 56
gregmatys Avatar answered Sep 30 '22 06:09

gregmatys