問題描述
我正在使用一個 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。