[resolved] How to add "px" to the_post_thumbnail ? (3 posts)

  1. gerald@WPcustoms
    Posted 3 years ago #

    An image calling with the_post_thumbnail has these attributes:
    height="100" width="200"
    what I require is:
    height="100px" width="200px"

    I found the function and modified it:

    function image_hwstring_px($width, $height) {
              $out = '';
            if ($width)
                     $out .= 'width="'.intval($width).'px" ';
             if ($height)
                     $out .= 'height="'.intval($height).'px" ';
             return $out;

    source: http://core.trac.wordpress.org/browser/trunk/wp-includes/media.php#L81

    so far nothing changed and the PX attribute is not added. If someone got an idea for this please share it :)

    reason for all this: IE8 cannot handle width\height values properly without "px" - life would be so easy without IE ;)

    Thanks for every tip.

  2. gerald@WPcustoms
    Posted 3 years ago #

    oh image_hwstring is no filter.

    do I have to modify the get_image_tag function instead?

    the $hwstring is called there
    $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />';

    but how can I change that?

  3. gerald@WPcustoms
    Posted 3 years ago #

    if someone comes across this problem in the future:

    this will solve it (old images added before the filter won't be affected)

    function custom_dimensions($html, $id, $alt, $title, $align, $size){
        $html = preg_replace('%(width|height)(="[0-9]{1,4})(")%', '$1$2px$3', $html);
        return $html;
    add_filter('get_image_tag', 'custom_dimensions',1, 6);

Topic Closed

This topic has been closed to new replies.

About this Topic