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.

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

  1. wpmhweb
    Posted 3 years 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


Topic Closed

This topic has been closed to new replies.

About this Topic