Support » Themes and Templates » Automatically reading the first image in an article as a thumbnail

  • Resolved okhalid

    (@okhalid)


    Hi,

    I am using a theme called “Brandford Magzine” and here is the link: http://www.wp-themes.der-prinz.com/magazine/a-featured-page/

    It uses a custom field called “leadimage” in each article where one have to specify an image URL to be displayed on the front page. The code it’s using is this:

    <img src=”<?php $values = get_post_custom_values(“leadimage”); echo $values[0]; ?> ” alt=”leadimage” id=”leadpic” width=”260″ height=”230″/>

    I am wondering is there a function in word press, rather then get_post_custom_values(“leadimage”) that allows one to read the first image in an article with out using the custom fields?

    Many thanks for your help!
    Omer

Viewing 6 replies - 1 through 6 (of 6 total)
  • I used this plugin: Get Image.

    Hi,

    Thanks for the plug-in information. But it didn’t really help. The way the theme is designed that it needs custom value “leadimage” to be set. So what i am thinking of is that once the simple post thumbnails upload the image, then it automatically creates a custom value for the post by setting “leadimage”. I think this will sort my issue..

    Any ideas of about how to do that?

    Thanks,
    Omer

    I have tried both gi_library(1) and wp_get_attachment_image( 1 )

    But no luck 🙁

    I have found the solution 🙂

    http://wordpress.org/support/topic/246893

    By adding the function to my functions.php:

    // Get URL of first image in a post
    function catch_that_image() {
    global $post, $posts;
    $first_img = ”;
    ob_start();
    ob_end_clean();
    $output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches);
    $first_img = $matches [1] [0];

    // no image found display default image instead
    if(empty($first_img)){
    $first_img = “/images/default.jpg”;
    }
    return $first_img;
    }

    I just needed to make the call

    <?php echo catch_that_image() ?>

    where I needed. This removed a dependency on external plugins and sorted out what I need 🙂

    Thanks a lot to all those who replied to my bost…

    OKHALID
    I am using a brother template to Bradford. I tried what you have posted here. Can you give me more details where in the code you placed the call?
    And where you placed the function?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Automatically reading the first image in an article as a thumbnail’ is closed to new replies.