WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How To Retrieve An Image Attachment's Alt Text? (14 posts)

  1. zitanix
    Member
    Posted 1 year ago #

    hello

    How To Retrieve Thumbnail Image Attachment's Alt Text?

    in version 3.4.2 i used this code :

    <?php
    $thumb_id = get_post_thumbnail_id($post->id);
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    if(count($alt)) echo $alt;
    ?>

    but after update to version 3.5 this code not working any more :(

  2. linux4me2
    Member
    Posted 1 year ago #

    I'm not certain this is it, but I would give it a try. You have the third parameter of get_post_meta set to true, which according to the function reference, tells it to return a string.

    Then you use the PHP count function to test for a result, but the count function is designed to be passed either an object or a string, so it may be causing a problem.

    There are a couple of ways to fix this, but since what you really want is a string, I would try the following:

    <?php
    $thumb_id = get_post_thumbnail_id($post->id);
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    echo $alt;
    ?>

    If there is no "alt" attribute for the given ID, you're going to get an empty string so you'll be echoing an empty string or the value you want.

  3. zitanix
    Member
    Posted 1 year ago #

    unfortunately not working :(

  4. linux4me2
    Member
    Posted 1 year ago #

    Hmmm...

    Are you getting any PHP errors? (An error_log file in the folder where this is running if you have "display_errors" off in your PHP configuration, the default on most production servers.)

  5. zitanix
    Member
    Posted 1 year ago #

    there is no PHP errors about this code

  6. linux4me2
    Member
    Posted 1 year ago #

    Where exactly are you running this code?

    Have you tested the results of $thumb_id to make sure you're getting a valid ID, then checked to make sure there is actually an "alt" attribute for it?

  7. zitanix
    Member
    Posted 1 year ago #

    in single-post.php

    <div id="best-post"  >
    <h3><?php _e('the best post','themater')?></h3>
    <ul>
    <?php
    global $post;
    $myposts = get_posts(array('numberposts' => 12, 'orderby' => 'rand' , 'post_status' => 'publish' , 'category' => 1427));
    foreach($myposts as $post) :
    setup_postdata($post);
    
    ?>
    <li>
    
    <?php
    if(has_post_thumbnail()) {
    echo '<a href="'.get_permalink().'">';
    echo get_the_post_thumbnail($post->ID, array(75,75) );
    echo '</a>';
    } ?>
    <span>
    <?php  /*
    $thumb_id = get_post_thumbnail_id($post->id);
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    if(count($alt)) echo $alt;
    */?>
    <?php
    
    $thumb_id = get_post_thumbnail_id($post->id);
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    echo $alt;
    ?>
    </span>
    </li>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    </ul>
    </div>
  8. lazyink
    Member
    Posted 1 year ago #

    I'm having a similar problem. I've done a var_dump and it's giving me a string of "string '' (length=0)" for each of the alt texts. There is definitely alt there but it doesn't seem to be finding it!

    global $post;
    			$args = array( 'numberposts' => 12, 'post_type' => 'clientes', 'orderby' => 'ASC');
    			$myposts = get_posts( $args );
    			foreach( $myposts as $post ) :	setup_postdata($post);
    
    			$alt_text = get_post_meta($post->ID , '_wp_attachment_image_alt', true);
    
    			?>
    			<li>
                    <!--BEGIN .hentry -->
                    <div class="post_box">
                        <div class="post-thumb left gallery">
                            <a href="<?php the_permalink() ?>">
    							<?php the_post_thumbnail('full'); ?>
    							<div class="overlay"><img src="/wp-content/uploads/2012/12/<?php echo $alt_text; ?>.jpg" /></div>
    						</a>
    
    					</div>
                    <!--END .hentry-->
                    </div>
    </li>
    <?php endforeach; var_dump($alt_text);?>
  9. linux4me2
    Member
    Posted 1 year ago #

    Is your code loop actually executing and displaying the thumbnail without the alt attribute, or is it not displaying the thumbnails at all; i.e., not executing?

    As I said above, I would check each variable through that code block by echoing the value of the variables to see where it's failing, because there are a number of places it could fail. Once you identify where it's failing, you can see if the function you're using has been changed for 3.5 or not.

  10. lazyink
    Member
    Posted 1 year ago #

    I did a dump of the entire $post and I couldn't find a ref to the alt text, but it is showing up with the thumbnails i'm pulling with the code.

    object(WP_Post)[2160]
      public 'ID' => int 1154
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2012-12-17 18:38:20' (length=19)
      public 'post_date_gmt' => string '2012-12-17 18:38:20' (length=19)
      public 'post_content' => string '' (length=0)
      public 'post_title' => string 'SBT' (length=3)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'closed' (length=6)
      public 'ping_status' => string 'closed' (length=6)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'sbt' (length=3)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2012-12-17 18:38:20' (length=19)
      public 'post_modified_gmt' => string '2012-12-17 18:38:20' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://localhost/wespanha/?post_type=clientes&p=1154' (length=57)
      public 'menu_order' => int 0
      public 'post_type' => string 'clientes' (length=8)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
  11. zitanix
    Member
    Posted 1 year ago #

    i found the solution :D

    this code working :

    <?php
    $thumb_id = get_post_thumbnail_id(get_the_ID());
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    if(count($alt)) echo $alt;
    ?>

    $post->id changed to <?php get_the_ID(); ?>

  12. Naved Ahmed
    Member
    Posted 1 year ago #

    Thanks zitanix

  13. Banahan
    Member
    Posted 10 months ago #

    Hi
    I saw you resolved your problem
    I have something similar
    I tried you code but it's not working for me...
    here is my code could you help please
    Thanks in advance

    <div id="fancy_gallery" style="display:none">
    <?php
    $pp_portfolio_enable_slideshow_title = get_option('pp_portfolio_enable_slideshow_title');
    
    foreach($all_photo_arr as $key => $photo)
    {
    	$full_image_url = wp_get_attachment_image_src( $photo->ID, 'full' );
    	$small_image_url = wp_get_attachment_image_src( $photo->ID, 'large' );
    ?>
    <?php
    $thumb_id = get_post_thumbnail_id(get_the_ID());
    $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    if(count($alt)) echo $alt;
    ?>
    <a id="fancy_gallery<?php echo $key; ?>" alt="<?php echo $alt; ?>" href="<?php echo $full_image_url[0]; ?>" class="fancy-gallery" <?php if(!empty($pp_portfolio_enable_slideshow_title)) { ?> title="<?php echo $photo->post_title; ?>" <?php } ?>></a>
    <?php
    }
    ?>
    </div>
  14. Franky57
    Member
    Posted 10 months ago #

    Hi !

    I would just make a little clarification

    get_the_ID() works very well

    but $post->id don't work because id is "case sensitive"

    id must be uppercase writed ... "ID" and not "id"

Topic Closed

This topic has been closed to new replies.

About this Topic