4

Hey everyone, I can't se the image of the post (it is attached through gravity form as a post-image) with this function I see the missing image icon with the title and the working link but no thumbnail image.? It is suposed to be more or less ( authors last 2 posts in the widget area) I suspect the problem is here :

$output .= '<img src="' .  wp_attachment_is_image( $post_id ) . '" alt="" />';

So please help me out :D

function get_related_author_posts() {
global $authordata, $post;

$authors_posts = get_posts( array( 'orderby' => 'rand', 'author' => $authordata->ID, 
'post__not_in' => array( $post->ID ), 'posts_per_page' => 2 ) );

$output = '<ul class="post_related">' . "\n";
foreach ( $authors_posts as $authors_post ) {
    $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '" title="' . 
apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '">';
    $output .= '<img src="' .  wp_attachment_is_image( $post_id ) . '" alt=""
/>';
    $output .= '</a></li>' . "\n";
}
$output .= '</ul>';

return $output;
}
2
  • wp-attachment_is_image only tells you if the attachment is an image - so it returns true or false.
    – anu
    May 20, 2011 at 9:23
  • any idea how to retrieve image from post than instead of that?
    – jimilesku
    May 20, 2011 at 14:57

2 Answers 2

1

Do I understand right that you mean featured thumbnail image for a post and not just any attached image?

See get_the_post_thumbnail() function and your usage will be something like this:

$output .= get_the_post_thumbnail( $authors_post->ID );
4
  • No I mean the image that was attached to the post via gravity form post image field. It is not a featured image, it's an attached image!!!
    – jimilesku
    May 21, 2011 at 18:13
  • @jimilesku I am not familiar with gravity forms, is attached to the post via gravity form post image field same as native WP attachment or not?
    – Rarst
    May 21, 2011 at 18:44
  • This code does not work :(
    – jimilesku
    May 22, 2011 at 22:27
  • I think it is...
    – jimilesku
    May 23, 2011 at 9:39
4

You can use wp_get_attachment_image_src() directly in your current code:

$output .= '<img src="' .  wp_get_attachment_image_src( $attachment_id, $size ) . '" alt="" />';

Or you can use wp_get_attachment_image() in place of your current code:

$output .= wp_get_attachment_image( $attachment_id, $size );

Note that in both cases you will have to retrieve the attachment ID in some manner. The most direct method will probably be get_posts(), e.g.:

$attachments = get_posts( array(
    'post_parent' => $post->ID,
    'post_type' => 'attachment',
    'post_mime_type' => 'image'
) );

Which will return an array of attachment objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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