WordPress.org

Ready to get started?Download WordPress

Forums

Auto use first image in post as "Featured Image" (11 posts)

  1. Mohd Yusuf
    Member
    Posted 3 years ago #

    Hi, i am wondering if it would be easy to auto take first image in the post as the featured image in latest WordPress 3.0.

    I searched for a plugin or guides; but i couldnt find anything useful for now.

    Anyone has some ideas on this? Some reference if i were to mod it myself (i know php, i just dont have wordpress function database in my head database :P )

    Thanks..

  2. Mohd Yusuf
    Member
    Posted 3 years ago #

    I ended up with my own function that i wrote to get the first image of the post and create a thumbnail on the fly.

    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      $new_img_tag = "";
    
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
    
      if(empty($first_img)){ //Defines a default image with 0 width
      $new_img_tag = "<img src='./images/bargainsfrenzy.com.gif' class='alignleft' />";
      }
    
      else{
        echo $new_img_tag = "<img src='./thumb.php?gd=2&src=".$first_img."&maxw=120' class='alignleft' />";
      }
    
      return $new_img_tag;
    }

    Then i hacked the WPTouch theme (i added the catch_that_image() into functions.php) and I hacked the WPTouch's index.php to call catch_that_image() instead.

    It works for me for now; I am still open for the 'featured image' solution though..

  3. mkearl
    Member
    Posted 3 years ago #

    I have been looking high and low for a plugin or something that can do this. Anyone have some ideas?

  4. Mohd Yusuf
    Member
    Posted 3 years ago #

    @mkearl, so far i havent seen one. Not sure i have time to make it a proper plugin. Maybe someone could improve the code into a plugin?

  5. mkearl
    Member
    Posted 3 years ago #

    Hey, give this a try (just found it). Seems to work pretty good for me right now.

    http://www.sanisoft.com/blog/2010/04/19/wordpress-plugin-automatic-post-thumbnail/

    --Matt

  6. Mohd Yusuf
    Member
    Posted 3 years ago #

    Sounds good. Will give it a try later.

  7. kkarpieszuk
    Member
    Posted 3 years ago #

    code in second post will cause lags, php doesnt like regexp, especially processed on big text content

    istead you should use http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

    to get id of attachment you can use function get_children
    http://codex.wordpress.org/Function_Reference/get_children

  8. Mohd Yusuf
    Member
    Posted 3 years ago #

    Thanks kkarpieszuk. That is very helpful.

  9. kannan921
    Member
    Posted 3 years ago #

    Thanks mkearl. Works Perfect with Pegasus theme.

  10. jimmerd
    Member
    Posted 3 years ago #

  11. Mohd Yusuf
    Member
    Posted 3 years ago #

    I tried http://wordpress.org/extend/plugins/auto-post-thumbnail/ but I wonder if I have to regenerate the thumbnail everytime i have a new post?

Topic Closed

This topic has been closed to new replies.

About this Topic