WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. 1xxooxx
    Member
    Posted 4 years ago #

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

    Basically I have this in my functions.php
    [code]
    /* 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 = '';
    ob_start();
    ob_end_clean();
    $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;
    }
    [/code]
    ----
    and in my single.php, archive.php, index,php I have..
    [code]
    <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">
    [/code]

    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?

  2. 1xxooxx
    Member
    Posted 4 years ago #

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

  3. archondigital
    Member
    Posted 4 years ago #

    I used something much simpler involving less lines of code see here
    http://wordpress.org/support/topic/234787?replies=18#post-1337817

    <?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 } ?>
  4. andykillen
    Member
    Posted 3 years ago #

    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="http://site.com/url/to/image.jpg" />

    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.

    http://wordpress.org/extend/plugins/share-and-follow/

    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
    Andy

Topic Closed

This topic has been closed to new replies.

About this Topic