gpc*_*ola 3 php wordpress jquery
我正在尝试为结果添加属性wp_get_attachment_image.
我想使用jquery lazyload来处理我的帖子缩略图的加载,为此我需要添加一个data-original=属性来创建<img>标签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) );
Run Code Online (Sandbox Code Playgroud)
但它没有像我预期的那样添加数据属性.
<img class="attachment-full" width="759" height="278" alt="..." src="..."></img>
Run Code Online (Sandbox Code Playgroud)
看看这个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;
}
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我没有测试过,但我认为问题是你的数组应该是第四个参数wp_get_attachment_image,而不是第三个参数.
所以
$placeholderimg = wp_get_attachment_image( 2897, "full", array('data-original'=>$imgsrc) );
Run Code Online (Sandbox Code Playgroud)
应该
$placeholderimg = wp_get_attachment_image( 2897, "full", false, array('data-original'=>$imgsrc) );
Run Code Online (Sandbox Code Playgroud)
假设您false对$icon参数的默认值()感到满意.