WordPress.org

Ready to get started?Download WordPress

Forums

How to remove the first image of a post only in the first paragraph (1 post)

  1. wpmhweb
    Member
    Posted 1 year ago #

    I currently have the code below to remove the first image from post. but this forces me to keep adding a first image every time to a post. I would like to be able to check if in the first paragraph of a post there is an image and if there is one, then and only then, make use the code below. This way I don't have to add a first image to a post anymore and i can use the feature image function.

    //Removes the first image on a post
    function remove_first_image ($content) {
    if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    $content = preg_replace ("/<p>\\s*?(<a><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s", "", $content, 1);
    } return $content;
    }
    add_filter('the_content', 'remove_first_image');
    //end of remove image

    thanks,

Topic Closed

This topic has been closed to new replies.

About this Topic