WordPress.org

Support

Support » Themes and Templates » [Resolved] How to add "px" to the_post_thumbnail ?

[Resolved] How to add "px" to the_post_thumbnail ?

  • 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;
     }
    add_filter('image_hwstring','image_hwstring_px');

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • oh image_hwstring is no filter.

    do I have to modify the get_image_tag function instead?
    http://core.trac.wordpress.org/browser/trunk/wp-includes/media.php#L223

    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?

    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);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] How to add "px" to the_post_thumbnail ?’ is closed to new replies.