3
我試圖給wp_get_attachment_image
的結果添加一個屬性。將屬性添加到wp_get_attachment_image
我想用jquery lazyload來處理我的帖子縮略圖的加載,並做到這一點,我需要爲<img>
標記添加data-original=
屬性wp_get_attachment_image
正在創建。
我已經試過:
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full");
$imgsrc = $imgsrc[0];
$placeholderimg = wp_get_attachment_image(2897, "full", array('data-original'=>$imgsrc));
但如我所料不添加數據屬性。
<img class="attachment-full" width="759" height="278" alt="..." src="..."></img>
望着wp_get_attachment_image
功能又好像這應該工作,雖然:
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
$html = '';
$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
if ($image) {
list($src, $width, $height) = $image;
$hwstring = image_hwstring($width, $height);
if (is_array($size))
$size = join('x', $size);
$attachment =& get_post($attachment_id);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags(get_post_meta($attachment_id, '_wp_attachment_image_alt', true))), // Use Alt field first
'title' => trim(strip_tags($attachment->post_title)),
);
if (empty($default_attr['alt']))
$default_attr['alt'] = trim(strip_tags($attachment->post_excerpt)); // If not, Use the Caption
if (empty($default_attr['alt']))
$default_attr['alt'] = trim(strip_tags($attachment->post_title)); // Finally, use the title
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters('wp_get_attachment_image_attributes', $attr, $attachment);
$attr = array_map('esc_attr', $attr);
$html = rtrim("<img $hwstring");
foreach ($attr as $name => $value) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
我要去哪裏錯了?
不用擔心。去過也做過。樂意效勞。 – Hobo