I’m working on building out a custom theme and have been struggling with this one for a bit. I am trying to modify the HTML output of the_post_thumbnail();
function. I need to do this because I am trying to support retina images on my site and would rather bake the functionality into my theme than load a plugin on the backend.
By default, the_post_thumbnail();
merely calls get_the_post_thumbnail();
which I found here. My first thought was to plug into the ‘post_thumbnail_html‘ filter, but I can’t seem to get it working. So…
This is how I’m calling my post thumbnails in the loop:
<?php the_post_thumbnail('custom-thumbnail-size', array('class' => 'unique-class-here', 'title' => 'unique-title-here')); ?>
This is the code I need output when I call the_post_thumbnail();
…
<img src="" alt="" data-src="image.png" data-alt="Alt text" class="retina unique-class-here" />
And the below code is what I currently have in my functions.php file:
<?php
function modify_post_thumbnail_html($html, $post_id, $post_thumbnail_id, $size, $attr) {
$src = wp_get_attachment_image_src(get_post_thumbnail_id(), $size);
$html = '<img src="" alt="" data-src="' . $src['0'] . '" data-alt="" class="retina" />';
return $html;
}
add_filter('post_thumbnail_html', 'modify_post_thumbnail_html', 99, 5);
?>
A couple things to note. I am not sure how to pass in the appropriate metadata text in the ‘data-alt’ attribute. Also, I need to be able to pass the specific post_thumbnail size I need since I use custom post_thumbnail sizes throughout my theme. Lastly, you can see the attribute array needs to pass in classes in addition to the default ‘retina’ class as well as any other attributes in that array.
Thanks in advance for any help. There’s really not much online about this topic and I figured my question was different enough to warrant an additional post to this community. Please let me know if you have any thoughts, solutions and/or need any clarifications.
Ok, so I think I may have come to a solution. It doesn’t seem as nice and easy as I’d like it to be, but then again major modifications to default WordPress functionality sometimes requires drastic measures. 🙂
This is my working solution to rewriting the HTML output for post thumbnails throughout my site to work with the Retinise.js plugin. Of course this code may be adapted for other post thumbnail HTML manipulations.
In my functions.php file, I created this function:
Then, whenever I call
the_post_thumbnail();
in a WP loop, I use this code:This code should work (obviously with minor modifications) if you’re operating outside of a WP loop. Hope this will save someone else some time and frustration! Maybe when I get around to it, I’ll post a complete start to finish Q & A on how I baked in retina support to my theme. No plugins used!
Here’s a few links to point someone in the right direction if interested!
hackish solution:
since wordpress add a lot of classes by default to the
<img>
, if you don’t change that behaviour by force, you can always ‘inject’ something via str_replace before the stringclass=
. In code:It’s pretty safe to assume that if something starts with “class=”, that’s what you want. Of course, it could be messed up by weird filenames containing that
class=
, but especially with wordpress, that’s highly unlikely.you could also search for
<img
and replace that; thinking about it, I guess that should be a bit safer.Maybe you could hook into
wp_get_attachment_image_attributes
:This needs to be tested and maybe amended, I found the idea here Add class name to post thumbnail (check for
vwp_get_attachment_image
source code at the end).You need to add the hook just before calling
the_post_thumbnail()
in your theme.Also, I don’t know what you need to use as
data-alt
, in my opinion, you could use a field of theattachement
that passes with the filter (it could be a custom field).