Forums

Get Alternate text for image with post thumbnail feature (5 posts)

  1. jumust
    Member
    Posted 1 year ago #

    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!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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>
  3. jumust
    Member
    Posted 1 year ago #

    Thanks!
    I'm not sure but the last line is correct without href and closing the ?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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>
  5. 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

  6. 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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags