問題描述

我正在使用一個 attachment.php 文件來顯示在其他地方點擊的大圖片。我想使用 javascript 將圖像替換為圖像替代文字,但是當使用 wp_get_attachment_image_src() 時,不包含 alt 文本。我不認為 WP 有一個檢索它的功能,所以我需要我自己的。要寫這個函數,我需要知道… 存儲圖像的 alt 文本在哪裏?

我的附件頁面使用不包含 alt 文本的 wp_get_attachment_image_src()

<div class = "entry">
<?php
if ( wp_attachment_is_image( $post->id ) ) :
    $att_image = wp_get_attachment_image_src( $post->id, "large");?>

    <a href="<?php%20echo%20wp_get_attachment_url($post->id);%20?>"
        title="<?php the_title(); ?>"
        rel="attachment">
    <img class="attached_img"
        src="<?php%20echo%20$att_image[0];?>"
        width="<?php echo $att_image[1];?>"
        height="<?php echo $att_image[2];?>"
        class="attachment-medium"
        alt="<?php $post->post_excerpt; ?>" />
    </a>
} <?php endif;?>
</div>

由此可見:

<div class = "entry">
    <a href="http://www.example.com/wp-content/uploads/2010/07/photo_namejpg"
       title="My_Photo_Title"
       rel="attachment">
       <img class="attached_img"
            src="http://www.example.com/wp-content/uploads/2010/07/photo_name_and_size.jpg"
            width="393"
            height="500"
            class="attachment-medium"
            alt="" />
    </a>
</div>

我知道 $post->post_excerpt 在上面的代碼中被調用,但是我不知道該怎麼替換它來獲取圖像的 alt 屬性。

最佳解決方案

我最近剛剛為一個客户項目做了一些研究,所以 lo-and-behold 我在這裏使用它!

在文本之後,您會看到 WordPress 3.0.1 中的圖像處理函數的大部分 (全部?) 分類列表 (我將它們分組在一些相似的順序中,但不要在我的分類中放置過多的信譽) 。

無論如何,回答 (我想) 你需要什麼,而不是你所要求的 (好的,我也會回答這個,最後) 我想你需要的是 wp_get_attachment_image()函數,它將返回一個包含這些屬性的 HTML 字符串:

  •  'src'

  •  'class'

  •  'alt'

  •  'title'


WordPress 3.0 圖像處理功能

所以這裏是 WordPress 的圖像處理函數,供您和其他人蔘考 (跳到下面的答案你的確切問題):

圖像支持/縮略圖

  •  set_post_thumbnail_size( $width = 0, $height = 0, $crop = FALSE )

  •  add_image_size( $name, $width = 0, $height = 0, $crop = FALSE )

  •  get_intermediate_image_sizes()

  •  wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 )

Attachment

  •  get_attached_file( $attachment_id, $unfiltered = false )

  •  is_local_attachment($url)

  •  update_attached_file( $attachment_id, $file )

  •  wp_attachment_is_image( $post_id = 0 )

  •  wp_count_attachments( $mime_type = '' )

  •  wp_delete_attachment( $post_id, $force_delete = false )

  •  wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '')

  •  wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false)

  •  wp_get_attachment_metadata( $post_id = 0, $unfiltered = false )

  •  wp_get_attachment_thumb_file( $post_id = 0 )

  •  wp_get_attachment_thumb_url( $post_id = 0 )

  •  wp_get_attachment_url( $post_id = 0 )

  •  wp_insert_attachment($object, $file = false, $parent = 0)

  •  wp_update_attachment_metadata( $post_id, $data )

MIME 類型

  •  wp_match_mime_types($wildcard_mime_types, $real_mime_types)

  •  wp_mime_type_icon( $mime = 0 )

  •  wp_post_mime_type_where($post_mime_types, $table_alias = '')

Uploads

  •  media_handle_upload()

Filesystem

  •  _wp_relative_upload_path( $path )

  •  wp_upload_dir( $time = null )

HTML

  •  get_image_tag($id, $alt, $title, $align, $size='medium')

低級圖像處理:

  •  wp_load_image( $file )

  •  image_constrain_size_for_editor($width, $height, $size = 'medium')

  •  image_downsize($id, $size = 'medium')

  •  image_get_intermediate_size($post_id, $size='thumbnail')

  •  image_hwstring($width, $height)

  •  image_make_intermediate_size($file, $width, $height, $crop=false)

  •  image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 )

  •  image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false)


按照承諾,圖像的'alt'文本以 wp_postmeta 的字符串存儲在'_wp_attachment_image_alt'的 meta_key 中。

您可能已經知道,可以使用簡單的 get_post_meta()加載它:

$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);

次佳解決方案

 Mike’s 答案是正確的,當然,但是 $alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true); 可能會返回一個空字符串。

 但是,wp_get_attachment_image 總是得到一個 alt_text 。

WordPress 團隊應用以下技巧,首先檢查 post_except,然後獲取標題。

if(empty($alt_text)) // If not, Use the Caption
{
    $attachment = get_post($post->ID);
    $alt_text = trim(strip_tags( $attachment->post_excerpt ));
}
if(empty($alt_text)) // Finally, use the title
{
    $attachment = get_post($post->ID);
    $alt_text = trim(strip_tags( $attachment->post_title ));
}

第三種解決方案

考慮看看 wp_prepare_attachment_for_js( $attachment ),其中 $attachment 是附件本身的 WP_Post 對象。

這是一個”kitchen sink” 函數,但它確實提供了一個非常好的散列與大量的元數據,包括’alt’:

$response = array(
        'id'          => $attachment->ID,
        'title'       => $attachment->post_title,
        'filename'    => wp_basename( $attachment->guid ),
        'url'         => $attachment_url,
        'link'        => get_attachment_link( $attachment->ID ),
        'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'author'      => $attachment->post_author,
        'description' => $attachment->post_content,
        'caption'     => $attachment->post_excerpt,
        'name'        => $attachment->post_name,
        'status'      => $attachment->post_status,
        'uploadedTo'  => $attachment->post_parent,
        'date'        => strtotime( $attachment->post_date_gmt ) * 1000,
        'modified'    => strtotime( $attachment->post_modified_gmt ) * 1000,
        'menuOrder'   => $attachment->menu_order,
        'mime'        => $attachment->post_mime_type,
        'type'        => $type,
        'subtype'     => $subtype,
        'icon'        => wp_mime_type_icon( $attachment->ID ),
        'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ),
        'nonces'      => array(
            'update' => false,
            'delete' => false,
            'edit'   => false
        ),
        'editLink'   => false,
        'meta'       => false,
    );

這是特別有用的 (顧名思義),通過 wp_send_ajax()將附件圖像元發送到 wp.media 視圖,但這並不意味着您不能將其用於其他目的。

我喜歡從_wp_attachment_image_alt 後期元字段中抽象出來,以便檢索替代文本的方法有所改變 (不太可能,但可以想到) 。

我覺得有一種 wp_get_attachment_image_alt()方法的情況。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。