0

I have a template tag of:

<?php echo the_post_thumbnail( 'full' ); ?>

It outputs:

<img src="/graphics/magazine/802/1.jpg" class=" wp-post-image" alt="" />

I want to remove 'class=" wp-post-image" ' from it.

My understanding of str_replace is little, and I've tried various 'guesses' but not successfully.

1

This function will return the featured image without the class attribute:

function wpse239577_post_thumbnail( $image_size = 'full' ) {
    $image_id = get_post_thumbnail_id();
    $image = wp_get_attachment_image_src( $image_id, $image_size );
    if ( ! $image ) {
        return false;
    }

    return '<img src="' . esc_url(  $image[0] ) . '" alt="" />';
}

Usage: echo wpse239577_post_thumbnail(); The $size parameter is a WordPress image size and is optional.

Also, the_post_thumbmail() already echos its output so echo is unnecessary. get_the_post_thumbmail() is similar, but it returns the output.

  • Thanks. Unfortunately it's not removing the unwanted stuff. I wonder whether this might be because it's generated by a plugin which enables an url to be set for the featured image rather than using the default function to set an image from the media library. (I don't use the media library, finding it preferable to photoshop an image and then ftp it to the server.) – gulliver Sep 17 '16 at 9:00
  • UPDATE... with a test page using a normal feature image, the code removes the various classes generated by WP, but still displays class="". – gulliver Sep 17 '16 at 9:15
  • My mistake! I misread your request. I've updated the answer. – Dave Romsey Sep 17 '16 at 9:22
  • 1
    Thanks, again. Appreciated. Works fine with a featured image set using the WP default, although not with an external feature image set by the plugin I'm using. No matter, the plugin is likely temporary anyway until I find a better way of adding a featured image not in the media library. – gulliver Sep 18 '16 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.