最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

images - replace wp_get_attachment_image with my own function

programmeradmin0浏览0评论

How can I replace wp_get_attachment_image() function without changing the core files. The function doesnt have an action hook or a filter hook.

What I am trying to achieve:

for lazyload plugin output the image html like this:

<img width="150" height="150" data-src="http://localhost/yxz/wp-content/uploads/2010/06/calliope.slide_-150x150.jpg" class="attachment-thumbnail" alt="calliope.slide" src="http://localhost/yxz/wp-content/uploads/blank.png">

instead of this:

<img width="150" height="150" src="http://localhost/yxz/wp-content/uploads/2010/06/calliope.slide_-150x150.jpg" class="attachment-thumbnail" alt="calliope.slide">

How can I replace wp_get_attachment_image() function without changing the core files. The function doesnt have an action hook or a filter hook.

What I am trying to achieve:

for lazyload plugin output the image html like this:

<img width="150" height="150" data-src="http://localhost/yxz/wp-content/uploads/2010/06/calliope.slide_-150x150.jpg" class="attachment-thumbnail" alt="calliope.slide" src="http://localhost/yxz/wp-content/uploads/blank.png">

instead of this:

<img width="150" height="150" src="http://localhost/yxz/wp-content/uploads/2010/06/calliope.slide_-150x150.jpg" class="attachment-thumbnail" alt="calliope.slide">
Share Improve this question edited Jun 6, 2013 at 13:04 Towfiq asked Jun 6, 2013 at 12:55 TowfiqTowfiq 4851 gold badge12 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

There is a filter, wp_get_attachment_image_attributes, for the image attributes-- a well designed one too.

function alter_att_attributes_wpse_102079($attr) {
  $attr['data-src'] = $attr['src'];
  return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'alter_att_attributes_wpse_102079');

That will add the data-src attribute. That looks like what you need. You could add more attributes, or alter the existing onese, if you need.

You can create another function in your functions.php file and then use it instead of wp_get_attachment_image().

发布评论

评论列表(0)

  1. 暂无评论