5 responses to “How to Add Thumbnails to WordPress with Custom Fields”

  1. Mariano Pagella

    Now this is something that wordpress has to work out better in a new release. It can be that difficult to show a simple thumbnail in a post. There should be a simple checkbox that says “show thumbnail with excerpt” or something like that.
    I tried the custom fields, but it turned out to be really painfull and not practical when posting lots of articles. So, searching around i came up with a simple function that makes it a lot more easy.
    Here it is:

    function showThumb() {
    global $post, $posts;
    $idPost = $post->ID;
    ob_start();
    ob_end_clean();
    //$images = get_children(‘numberposts=1&post_parent=’.$idPost.’&post_type=attachment&post_mime_type=image’);

    $images = get_children( array(
    ‘post_parent’ => $idPost,
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’,
    ‘orderby’ => ‘menu_order ASC, ID’,
    ‘order’ => ‘ASC’)
    );

    if ($images) {
    $images_data = array_keys($images);
    $image = wp_get_attachment_image_src($images_data[0], ‘thumbnail’);
    $thumb = $image[0];
    } else {
    $thumb = bloginfo(‘stylesheet_directory’).”/images/thumb_default.jpg”;
    }
    return $thumb;
    }

    Ir works like this: First creates an array with all the attachments of your posts, orders it by position and retrieves de first one, so the first image in your post (your can set up the position when you add the picture, like when you create a galley) Now, if there are no attachments, then it returns the default thumb’s url.
    The only thing you got to do now is place inside The Loop, and it will retrieve the thumbnail url. So you dont need to set up custom fields or anything like that.

    But anyway, wordpress should really work something about this in some upcoming release.

  2. Neeraj

    Thumbnail icon is coming bt thumbnail not.. plz help.. how the thumbnail will appear.
    Thanks

  3. Hector

    Hi, Great Post. Really helped me with setting thumbnails.

    I did have a question though. I’m using the Arras Theme. I’ve been successful at making a post and having a thumbnail of that post show up on the main page in the galleries.

    But what i can figure out is how to make it so that the thumbnail Only shows up in the main page, but not show up in the individual post?

    Please help.

    Thanks!

  4. Bex White

    Excellent, that worked first time for me, I have been playing around adding widgets and apps trying to figure this out having no idea that it was such a simple job. The more I experiment with WordPress and customise it, the more I love the platform. Every minor problem or niggle I encounter is usually just my not knowing that there is a simple solution ready made and built in already.

Leave a Reply