Support » Plugins » Scan Article for First Image, or look for Custom Field (functions.php)

  • Scan Article for First Image, or look for Custom Field (functions.php)

    Basically I have this in my functions.php
    /* Catche First Image on Post
    CURRENT: <?php echo catch_that_image() ?>
    REPLACED: <?php get_thumb($post->ID, ‘full’); ?>
    function catch_that_image() {
    global $post, $posts;
    $first_img = ”;
    $output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(empty($first_img)){ //Defines a default image
    $first_img = “/images/default.jpg”;
    return $first_img;
    and in my single.php, archive.php, index,php I have..
    <img src=”<?php bloginfo(‘template_directory’); ?>/includes/phpThumb/phpThumb.php?src=<?php echo catch_that_image() ?>&h=249&w=250&q=50&f=jpg&zc=1&sia=thenext2shine-<?php the_date(‘Y-m-d’); ?>
    .jpg” alt=”<?php the_title(); ?>” class=”articleimage”>

    However I want to first check my CUSTOM FIELD “article_thumb” for the image.. and IF NOT.. then use the FIRST image in the article. How can I do this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I just want to do an IF STATEMENT

    look for first image in article
    if there is no image in the article ->>
    look for the custom field “article_thumb” ->>
    if there is no “article thumb” set “default.jpg” as the article image.
    end if

    I think all that can be done in the functions.php without altering the code in any other file..

    I used something much simpler involving less lines of code see here

    <?php $postimageurl = get_post_meta($post->ID, 'post-thumb', true);
    if ($postimageurl) {
    <img src="<?php bloginfo('url') ?>/wp-content/themes/themename/timthumb.php?src=<?php echo $postimageurl; ?>&h=176&w=136&zc=1&q=100" alt="<?php the_title(); ?>" width="136" height="176"/>
    <?php } else { ?>
    <img src="<?php bloginfo('url') ?>/wp-content/themes/themename/timthumb.php?src=<?php bloginfo('url') ?>/wp-content/uploads/default.jpg&h=176&w=136&zc=1&q=100" alt="<?php the_title(); ?>" width="136" height="176"/></a>
    <?php } ?>


    Hey then2s,

    If your interested at all any more in this, I’ve setup my plugin “Share and Follow” to scan through the image attachments inside the WordPress Media Library for the first available thumbnail.

    From that thumbnail it set’s up the following item in the head section

    <link rel=”image_src” href=”” />

    This is what is needed for Facebook to put an image next to a post inside the Newsfeeds.

    It also allows for a custom field called “image_src” with a specific URL as the value, which takes precedence over in Media Library.

    It will also if there is no image or custom field “image_src”, it will default back the Author Gravatar image.

    This is configurable at a Page, Post, Homepage, Archive page level

    check out the video if you want to see what goes on.
    All the best

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Scan Article for First Image, or look for Custom Field (functions.php)’ is closed to new replies.