• Hi all,

    I know this has been covered before, but it’s been a while, and I can’t find a comprehensive solution.

    My client needs to remove the first image and its caption from all posts. (Ideally, permanently.)

    If any of you lovely and generous PHP-massagers out there would whip up some new code, or tweak what’s below to work, I’d be super-super grateful. And I’m sure a lot of other people would, too. Here’s what I’ve got:

    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 removes the first image from displaying on the front-end, but leaves it in the Editor. That’s bound to confuse my client, so if possible, I’d like to delete it permanently. It also doesn’t remove the caption. I found this, which targets the caption at the bottom of the post linked above; I just don’t know how to marry it to what’s above:

    echo preg_replace("/\[caption .+?\[\/caption\]|\< *[img][^\>]*[.]*\>/i","",get_the_content(),1);
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove first image and caption from posts?’ is closed to new replies.