我正在尝试为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) );
<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; }
我哪里错了?
解决方法
我没有测试过,但我认为问题是你的数组应该是
wp_get_attachment_image
的第四个参数,而不是第三个.
所以
$placeholderimg = wp_get_attachment_image( 2897,array('data-original'=>$imgsrc) );
应该
$placeholderimg = wp_get_attachment_image( 2897,false,array('data-original'=>$imgsrc) );
假设您对$icon参数的默认值(false)感到满意.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。