The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] How to remove first image from the_content? (7 posts)

  1. Alkorr
    Posted 5 years ago #

    Hi. I would like to remove the first image from the_content. It's not an easy task but I've found a piece of code on this website but it looks heavy (the author says 'It is not an elegant solution, but it works') and it doesn't work on my site (I get: Warning: preg_replace() [function.preg-replace]: Unknown modifier 'I' in /homez.32/mysite/www/bonto/wp-content/themes/v3/functions.php on line 60)...

    Does someone knows how to skip the first image and just show the_content without the first image?

    Thanks a lot for your help! :)

  2. chaseman
    Posted 5 years ago #

    Did you try get_the_content? AFAIK it will simply take the raw text. Give it a try, and don't forget to echo it out. :)

  3. Alkorr
    Posted 5 years ago #

    Hi chaseman, thanks but using get_the_content function does the same as the_content. I'm using it on single.php.

    I searched again today and I can't find a way to get ride of the first image in the_content.

    Still looking...

  4. vinay
    Posted 5 years ago #

    Hi Alkorr,
    You can use the following code in place of the_content();

    echo preg_replace("/\< *[img][^\>]*[.]*\>/i","",get_the_content(),1);

  5. Alkorr
    Posted 5 years ago #

    Hi vinay! This code is amazing!! No need for to call a function, your code only is enough to do exactly what I want... Thank you so much for your help! :)

  6. Lynn
    Posted 5 years ago #


    Oh my. I have been looking for something like this for a long time. So many themes have no sensible way to limit images in archives. I get both a thumbnail and the full-size image. Until now. Thanks for this code.


  7. talkstory
    Posted 4 years ago #

    Very helpful! I had a mix of images and images with captions so I modified the code to remove the caption info as well:

    echo preg_replace("/\[caption .+?\[\/caption\]|\< *[img][^\>]*[.]*\>/i","",get_the_content(),1);

Topic Closed

This topic has been closed to new replies.

About this Topic