Removing the first image in content


I’ve been using this code:

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

for a few years now on my site that has almost 20,000 posts with images inserted at the top. I have a plugin that uses the first image as a featured image as well. sometimes I need to still insert a first image into content so it doesn’t remove another image I have posted in there.

I would like to have the first image in the content removed completely in posts. so that in the future I don’t have to insert a featured image into the content every time, just so other images show up when I have more than one image. So far I haven’t been able to find anything on this except having to go into 20,000 posts and remove the first image.

Any ideas?

, , , Bryan Kremkau 4 years 2020-03-06T15:52:29-05:00 0 Answers 111 views 0

Leave an answer