How can I get the alternate text of the thumbnail I set up in the post, I mean here. In the website source code it's just "alttext" for all images.
Here it is the code I use to get thumbnail and link it to the post:
<?php
$img_id = get_post_thumbnail_id($post->ID); // This gets just the ID of the img
$image = wp_get_attachment_image_src($img_id, $optional_size); // Get URL of the image, and size can be set here too (same as with get_the_post_thumbnail, I think)
$perm = get_permalink($post->ID);
?>
<a href="<?php echo($perm); ?>" title="<?php the_title(); ?>"><img src="<?php echo($image[0]); ?>" width="<?php echo($image[1]); ?>" height="<?php echo($image[2]); ?>" class="alignleft" alt="alttext" /></a>
I'll appreciate any helps
Thanks!
try this
<?php
$img_id = get_post_thumbnail_id($post->ID); // This gets just the ID of the img
$image = wp_get_attachment_image_src($img_id, $optional_size); // Get URL of the image, and size can be set here too (same as with get_the_post_thumbnail, I think)
$alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true);
$perm = get_permalink($post->ID);
?>
<a>" title="<?php the_title(); ?>"><img src="<?php echo($image[0]); ?>" width="<?php echo($image[1]); ?>" height="<?php echo($image[2]); ?>" class="alignleft" alt="<?php echo $alt_text; ?>" /></a>
Thanks!
I'm not sure but the last line is correct without href and closing the ?
Oops, sorry that should be:
<a href="<?php echo($perm); ?>" title="<?php the_title(); ?>"><img src="<?php echo($image[0]); ?>" width="<?php echo($image[1]); ?>" height="<?php echo($image[2]); ?>" class="alignleft" alt="<?php echo $alt_text; ?>" /></a>
anotherandrew
Member
Posted 1 year ago #
I am a little confused.
I am trying to get the alt text for the featured image and then return it into a link in a
tag.
I have
function thumbnail_navigation(){
echo '<ul class="thumb-nav-list">';
foreach( ( get_the_category() ) as $category ) {
$the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5');
while ($the_query->have_posts()) : $the_query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
<?php echo '</ul>'; ?>
<?php
}
}
add_filter('get_category_posts' , 'thumbnail_navigation');
And need this "<?php the_title(); ?>" to return the alt text of the featured image.
Any thoughts?