WordPress.org

Ready to get started?Download WordPress

Forums

Get a first image of a post with preg_match_all (1 post)

  1. Romik84
    Member
    Posted 4 years ago #

    Hi there,

    I would need a help with preg_match_all function. I try to get first image of a post for related posts plugin. Currently I use following function:

    function get_first_image() {
     global $post, $posts;
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches);
     $first_img = $matches [1][0];
     return $first_img;
    }

    This function is working without a problem. The problem is that some posts are not in the wp_posts table so it doesn't show any picture for some items. Some posts contents are in wp_post_settings table created by a plugin where new posts contents are saved. How should the function looks now to work for both (for classic post content in the wp_posts as well as for wp_post_settings.

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic