Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can anyone help me develop a wordpress script that [IF] will get first image post and if it doesn't exist it will [ELSE] get first attached (uploaded) image.

Theoretically: [IF exists]

<img src="<?php echo catch_that_image() ?>" />

[or ELSE]

<img src="'. wp_get_attachment_url($attachment->ID) . '" alt="Main Image" id="mainImage"" />

[or ELSE]

<img src="<?php bloginfo('template_url') ?>/images/default.png" />

My current code looks like this (UPDATED.. AGAIN)

<?php  
 $args = array(
 'post_parent' => $post->ID,
 'post_type' => 'attachment',
 'post_mime_type' => 'image',
 'orderby' => 'menu_order',
 'order' => 'ASC',
 'offset' => '0',
 'numberposts' => 1 
 );
 $attachments = get_posts($args);
 $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
 if ($attachments) {
 foreach ($attachments as $attachment) {
 if(wp_attachment_is_image( $attachment->ID )) {
 echo '<img src="'. wp_get_attachment_url($attachment->ID) . '" alt="Main Image" id="mainImage"" />'; }}} 
 <!-- else if ( count( $images ) > 0 ) echo get_the_post_thumbnail; } -->
 else {
 echo '<img src="http://hellotokyostore.com/wp-content/themes/hellotokyo/images/default.png" alt="Hello Tokyo" />'; }
?>
share|improve this question
    
Could you explain what this is for dan? Sounds like you're trying to take the post's first image and make it into a thumbnail, but if no image exists, to use a default thumbnail image? If so, there's a better way of going about it :) –  dunc Feb 8 '12 at 9:23
    
Hey Dunc! Basically I need something that will first try to grab the first image on the post (linked image “<img src"). If that doesn't exist, it will try to grab the first image uploaded/attached to the post. And if that doesn't exist, it will grab a default image (example default.jpg). Is this possible?! Please help! –  dan Feb 8 '12 at 9:28
    
You can see what I'm trying to do here: hellotokyostore.com/locations –  dan Feb 8 '12 at 9:29
    
Ah, yes, as I thought - you're after a thumbnail? Have you considered adding a "Featured Image" to your posts? I'll add an answer now. –  dunc Feb 8 '12 at 9:32
add comment

2 Answers

Check the Get The Image plugin by Justin Tadlock, it offer many options and fallbacks for getting images.

share|improve this answer
    
Thanks for the heads up Mamaduka! I already tried this and it didn't work.. :( –  dan Feb 8 '12 at 10:18
    
What was the problem? Maybe I can help with configuration. –  Mamaduka Feb 8 '12 at 10:19
    
It is now looking like this: hellotokyostore.com/locations –  dan Feb 8 '12 at 10:41
add comment

Updated answer

Updated the code from dan's comment on this post.

<?php 

    $args = array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'offset' => '0',
        'numberposts' => 1 
    );

    $images = get_posts($args);

    if ( count( $images ) > 0 ) {

        echo '<img src="'. wp_get_attachment_url($images[0]->ID) . '" alt="Main Image" id="mainImage"" />';

    } else {

       echo '<img src="http://hellotokyostore.com/wp-content/themes/hellotokyo/images/default.png" alt="Hello Tokyo"/>';

    }

?>

This information is now out of date but may be useful to another.

I think what you'd like to do is this:

  • Each post has a thumbnail
  • If no thumbnail is attached, use a default image

I've adapted code from this address. Have a look at it so you can understand how "Featured Images" work.

The gist is that you'll have a new box on your post page called "Featured image" where you can set a "thumbnail" for each post.

Add the following to your theme's functions.php.

<?php

if (function_exists('add_theme_support')) {
    add_theme_support('post-thumbnails');
    /* change these dimensions to suit your website.
       noticed you used 250 * 250 on the page you linked in
       OP comments, so I've used that. you may want to increase
       the size of your individual page's thumbnails */
    add_image_size('index-categories', 250, 250, true);
    add_image_size('page-single', 250, 250, true);
}

function InsertFeaturedImage($content) {

    global $post;

    $original_content = $content;

    $default_thumbnail = get_bloginfo('template_directory') . '/images/default_thumbnail.png';
    $default_src = '<img src="' . $default_thumbnail . '" alt="Your caption here" />';

    if ( current_theme_supports( 'post-thumbnails' ) ) {

        if ( ( is_page() ) || ( is_single() ) ) {
            /* if you're viewing a single post
               250 * 250 as designated above */

            if ( has_post_thumbnail() ) {
                $content = the_post_thumbnail('page-single');
            } else {
                $content = $default_src;
            }

            $content .= $original_content;
        } else {
            /* if you're viewing a list of posts, use this size of thumbnail
               250 * 250 as designated above */

            if ( has_post_thumbnail() ) {   
                $content = the_post_thumbnail('index-categories');
            } else {
                $content = $default_src;
            }

            $content .= $original_content;
        }

    }

    return $content;
}

add_filter( 'the_content', 'InsertFeaturedImage' );

?>

Please don't hesitate to ask if any part of this doesn't make sense.

share|improve this answer
    
Hey Dunc! Thanks for this script. But I'm looking for something that won't require to use "Feature Image". Because alot of times, I just take a picture using Instagram (via iphone) and it automatically posts it to Wordpress! This allows me to quickly upload and update the website without having to login to the site all the time :) –  dan Feb 8 '12 at 9:51
    
Try my updated answer. –  dunc Feb 8 '12 at 10:02
    
Am I doing something completely wrong? Placed the script in function.php . Then put <?php echo displayThumbFromImages() ?> and now getting an error. –  dan Feb 8 '12 at 10:10
    
You shouldn't need to use the function itself dan; the filter add_filter ('the_content', 'displayThumbFromImages'); should do the trick (put the code in functions.php). I've updated my code a little, try refreshing. –  dunc Feb 8 '12 at 10:22
    
Hmm.. now its doing something really different. It's placing the image in content area and repeating itself on other posts. Also where the picture needs to go is now empty. Please see the site for reference hellotokyostore.com/locations –  dan Feb 8 '12 at 10:34
show 27 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.