dtlh12053 2013-07-26 17:49
浏览 81

在PHP中更改the_post_thumbnail的输出

I am brand new to PHP. I've messed with wordpress on it but not really with the code behind it. I am trying to speed up the loading time of our wordpress site but I am having some trouble. PHP uses the_post_thumbnail to load the HTML code for some of the posts' images. I have this code to delay loading of the images a little so the page can load first and then the images will load:

jQuery(window).load(function() {
jQuery('img[source]').prepend(function(){
    var source = jQuery(this).attr('source');
    jQuery(this).attr('src', source);
});
});

This works very well on another site I work on written in coldfusion and normal html. So what I'm trying to do is change the output of the_post_thumbnail to have a 'source' attribute that has the actual source path and the initial src value will have a default loading gif.

My first thought was to build another function or I guess as PHP calls it, add a filter like this:

add_filter('post_thumbnail_html', 'thumbnail_filter');
function thumbail_filter() {
$default_attr = array(
    'src'   => $src,
    'class' => "attachment-$size",
    'alt'   => trim(strip_tags( $wp_postmeta->_wp_attachment_image_alt )),
);
the_post_thumbnail($default_attr);
// you can alter the resulted HTML here
$html = the_post_thumbnail($default_attr);
return $html;
}

This didn't actually broke the page and didn't show any images even when I haven't called the thumbnail_filter function.

My next guess as to what to do was this. Can someone who is a little more experienced in PHP and wordpress help me out? Maybe the first one is the right way to go but I'm just doing it wrong.

  • 写回答

1条回答 默认 最新

  • douping5015 2013-07-29 11:23
    关注

    The thumbnail_filter() function will get called automatically when the the_post_thumbnail() function will get called. and to add a source attribute to the img tag you have to replace your filter with this filter

    add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
    
    function my_post_image_html( $html, $post_id, $post_image_id ) {
    
      $html = '<img src="" source="'.get_permalink( $post_id ).'"/>';
      return $html;
    
    }
    

    paste this code in your functions.php file. I guess this will work.

    评论

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?