With the introduction of Featured image in wordpress, attaching images to your post is not necessary. Most of the templates and plugins now work with Featured Images. So if you have upgraded your wordpress to newer version and want to use featured image, it will be a daunting task to set featured image for every post that were written before the upgrade.

But for that you have a plugin called Media Tools that will set featured image for every post from the first image attached to the post. Even if the first image is from external source like Flickr or Photobucket, then it will download and add them to your media gallery and set them as featured image.

But after setting featured image for every post, your attached images inside the post will remain, and with certain templates you will have same images displayed twice – once for the feature image and once for the attached image. So in that case you may want to remove the first attached image from the post. You can do it by adding these lines to your functions.php which is located in your theme folder.

Usually it is located in http://yourdomain/wp-content/themes/yourtheme/functions.php

function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
} return $content;
add_filter('the_content', 'remove_first_image');

This code will search the content and remove the first image from your posts. Here preg_replace() searches for the image tag and removes the first occurrence from the content.

Remove first image from wordpress posts
Tagged on:                 


3D modeler and Web designer. Founder of Techlister. Love to build sites using Wordpress and Joomla. Interested in Astronomy.

4 thoughts on “Remove first image from wordpress posts

  • at

    This code works great!
    How to also include the caption – I can remove images, but captions remain?

    • at

      we have to search for the html element that contains the caption and remove it, like we did for the

  • at

    The first image was successfully hidden. Thank you. Please also include how to delete the caption of the first image. Because the first image caption is still left behind. please, thank you very much.


Leave a Reply

Your email address will not be published.