WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Automatically reading the first image in an article as a thumbnail (7 posts)

  1. okhalid
    Member
    Posted 5 years ago #

    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

  2. stvwlf
    Member
    Posted 5 years ago #

  3. nerdanita
    Member
    Posted 5 years ago #

    I used this plugin: Get Image.

  4. okhalid
    Member
    Posted 5 years ago #

    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

  5. okhalid
    Member
    Posted 5 years ago #

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

    But no luck :-(

  6. okhalid
    Member
    Posted 5 years ago #

    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...

  7. newportlegal
    Member
    Posted 4 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic