0

Hi i'm using this code below, which links the title of the post to the first attachment in the post. But how do I change it so that it grabs the thumbnail instead?

<?php  
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'offset' => 0,
'orderby' => 'menu_order',
'order' => 'asc',
'post_status' => null,
'post_parent' => $post->ID,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
    if(wp_attachment_is_image( $attachment->ID )) {
echo '<a href="'. get_attachment_link($attachment->ID) . '">'. get_the_title() .'</a>';

    break;
   } 
}
}
else {
echo ''. get_the_title() .'';
};
?>

EIDT: Just as the title link goes to the attachment page of the first image, i want the thumbnail to do the same.

so instead of '.get_the_title().'</a>' i want to get the thumbnail attached to the post.

1
  • You want to grab link to first attachment's thumbnail or link to post thumbnail? Jun 30, 2013 at 18:59

3 Answers 3

1

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();
}
2
  • I wanted to get the thumbnail of the post and make it link to the first attatchment under that post :) @s_ha_dum
    – user32447
    Jun 30, 2013 at 20:45
  • @user32447 : See the edit.
    – s_ha_dum
    Jun 30, 2013 at 20:58
0

If you want title to link to post thumbnail instead of first attachment, then something like this should help:

if ( has_post_thumbnail() ) {
    echo '<a href="'. get_attachment_link( get_post_thumbnail_id() ) . '">'. get_the_title() .'</a>';
} else {
    echo get_the_title();  // you don't have to concatenate it with empty strings
}
0

Just replace:

echo '<a href="'. get_attachment_link($attachment->ID) . '">'. get_the_title() .'</a>';

with:

echo wp_get_attachment_link( $attachment->ID , '', true, false, get_the_post_thumbnail( get_the_ID, 'thumbnail') ) 

You find further information how to customize the output of wp_get_attachment_link() in the codex: http://codex.wordpress.org/Function_Reference/wp_get_attachment_link .

It's always very helpful to check the used function in codex and scroll to the bottom, where you can find related functions: http://codex.wordpress.org/Function_Reference/get_attachment_link#Related

Edit:

There is one small piece you could edit on s_ha_dums solution to make it even more cleaner.

Replacing:

$title = '<a href="'.get_attachment_link($attachments[0]->ID).'">'.$thumb.'</a>';

with:

$title = wp_get_attachment_link( $attachments[0]->ID , '', true, false, $thumb );

would use WordPress' function for getting the attachements link using the post-thumbnail as link.

6
  • And how is it related to original problem? You only change method of outputting link, but it still links to attachment (and not to post thumbnail)... Jun 30, 2013 at 18:50
  • @KrzysiekDróżdż : I can't tell if the OP wants the thumbnail image itself or a link to the thumbnail image so I think this is a plausible answer, at least for now.
    – s_ha_dum
    Jun 30, 2013 at 18:53
  • @s_ha_dum: But this answer doesn't return link to thumbnail thumbnail - it still returns link to first image attachment, only in different way. Jun 30, 2013 at 18:57
  • @KrzysiekDróżdż : Yes, I know but that is part of what is unclear to me about the question. At the moment, I think your answer is equally plausible.
    – s_ha_dum
    Jun 30, 2013 at 19:00
  • OK, I understood your problem few seconds after posting my comment :) Jun 30, 2013 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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