The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Get 'Alternate Text' from attachments (6 posts)

  1. iLightning
    Posted 6 years ago #

    I'm looping through image attachment to display images and thumbnails for a portfolio page using WordPress (of course). I'm trying to pull the data from the new "Alternate Text" field available for WP 2.9 but I've been unable to find where this data is stored.

    I'm able to get data from the other fields using the below code but where is 'Alternate Text' field info?

    Here is what is entered in the WP back-end for this image:
    Title: Personal1L
    Alternate text: Personal 1 Alt Text
    Caption: Personal 1 Caption
    Description: Personal 1 Description
    Link URL: (its blank)

    Here is the output of the below code for this image:
    ID: 53
    post_author: 1
    post_date: 2010-01-27 06:33:52
    post_date_gmt: 2010-01-27 06:33:52
    post_content: Personal 1 Description
    post_title: Personal1L
    post_excerpt: Personal 1 Caption
    post_status: inherit
    comment_status: closed
    ping_status: open
    post_name: personal1l
    post_modified: 2010-01-27 06:33:52
    post_modified_gmt: 2010-01-27 06:33:52
    post_parent: 30
    guid: http://localhost/sitename/wp-content/uploads/Personal1L.jpg
    menu_order: 4
    post_type: attachment
    post_mime_type: image/jpeg
    comment_count: 0
    filter: raw

    And here is relevant code:

    <!--This is happenening inside the Loop-->
    $args = array(
      'post_type' => 'attachment',
      'numberposts' => -1,
      'post_status' => null,
      'post_parent' => $post->ID,
      'post_mime_type' => 'image',
      'orderby' => 'menu_order',
      'order' => 'ASC',
    <?php $attachments = get_posts($args); ?>
    <?php if ($attachments) { ?>
    	<?php foreach ($attachments as $attachment) { ?>
    		<!--Loop through attachements (images) -->	
    		<?php foreach ($attachment as $key => $value) {?>
    		  <!--Loop through each attachement (image) and display all associated key : value pairs-->	
              <p><?php echo $key; ?>: <?php echo $value ?></p>
    		<?php }; // nested foreach ?>
    		<!--End loop through attachements-->
    	<?php } //foreach ?>
    <?php } //if ?>


  2. kz
    Posted 6 years ago #

    $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
    if(count($alt)) echo $alt;
  3. iLightning
    Posted 6 years ago #

    Great, worked perfectly.

    Why if(count($alt))?

  4. kz
    Posted 6 years ago #

    If '_wp_attachment_image_alt' is not set,
    echo $alt; shows text like 'Array()'.

  5. DDT
    Posted 6 years ago #

    Can i get this to work for a custom field as well??

  6. Worked for me. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic