• Hello all,
    I’ve browsed the forum but found no reply.

    the question is: is it possible to get the thumbnail of an image (in post) in the home page just with template tags, without using plugins and / or custom fields?

    thumbnails are just loaded from the post id and show up in the home page

    I’ve tried the wp_get_attachment_link, but had no success.

    Thanks in advance for help,
    ciao!

Viewing 6 replies - 1 through 6 (of 6 total)
  • I use this in my Get the Image plugin:

    <?php
    
    global $post;
    $default_size = 'thumbnail';
    
    $attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'numberposts' => 1) );
    if($attachments) :
    	foreach($attachments as $id => $attachment) :
    		$img = wp_get_attachment_image_src($id, $default_size);
    	endforeach;
    endif;
    ?>

    This should return you a variable of $img, which is the thumbnail URL of the first image (ordered in media uploader). This should be within the Loop.

    There are no template tags for something like this.

    Thread Starter neuville

    (@neuville)

    hello Greenshady,
    thanks for your quick reply. just tested but not working, no images showing up in the homepage…

    Sorry, meant to say the img URL would be $img[0] and not $img as I said before.

    Using that works perfectly.

    Thread Starter neuville

    (@neuville)

    Hello greenshady, just tested, it echoes just the blog main address, with no images attached.

    this is the code I’m using:

    <?php

    global $post;
    $default_size = 'thumbnail';

    $attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID', 'numberposts' => 1) );
    if($attachments) :
    foreach($attachments as $id => $attachment) :
    $img[0] = wp_get_attachment_image_src($id, $default_size);
    endforeach;
    endif;
    ?>

    is there any hack I’ve to do with it?

    Note:
    $default_size = ‘thumbnail’;
    $default_size = ‘medium’;
    $default_size = ‘full’;

    for the ‘full’ size, the max width is 500 and if you want to change this insert next line :
    $GLOBALS[‘content_width’] = ‘your full size’;

    Thanks Justin! Just what I needed.

    I know this is a very old post, but for the benefit of others, you put Justin’s code within the loop as is. Then to call the thumbnail, use.

    <?php if (!$img[0]) { echo 'Default image'; } else { ?><img src="<?php echo $img[0]; ?>" /><?php } ?><?php the_excerpt() ; ?>

    You will see that I have an additional if else condition to check to see if the post does have attachments and display a default.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Add thumbnail in home page without plugins’ is closed to new replies.