Function to call the attachment image from post

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 😀

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;
}

Related posts

Leave a Reply

2 comments

  1. 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.