I can’t tell if you want a link to the thumbnail or you want the image to be the thumbnail.
To get the image assigned to the post as the thumbnail image, use get_the_post_thumbnail
:
$thumb = get_the_post_thumbnail($post_id, 'thumbnail');
To get the thumbnail using the attachment ID use wp_get_attachment_image
:
$thumb = wp_get_attachment_image( $attachment_id, 'thumbnail' )
Not that that will return an empty string if the requested image does not exist.
To get a link to the image using the attachment ID use wp_get_attachment_thumb_url
$thumb = wp_get_attachment_thumb_url( $attachment_id );
Or wp_get_attachment_image_src
:
$thumb = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
if (!empty($thumb['url'])) {
$thumb_url = $thumb['url'];
}
Based on a comment below: How to “get the thumbnail of the post and make it link to the first attatchment under that post”
$thumb = get_the_post_thumbnail($post->ID, 'thumbnail');
if (!empty($thumb)) {
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_status' => 'inherit',
'post_parent' => $post->ID,
);
$attachments = get_posts($args);
if (!empty($attachments[0]->ID)) {
if(wp_attachment_is_image( $attachments[0]->ID )) {
$title="<a href="".get_attachment_link($attachments[0]->ID).'">'.$thumb.'</a>';
}
}
}
if (!empty($title)) {
echo $title;
} else {
the_title();
}