问题描述

我正在使用一个 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。