WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Alkorr
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 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
    Member
    Posted 3 years ago #

    vinay,

    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.

    Lynn

  7. talkstory
    Member
    Posted 2 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