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;
}
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:You can use
wp_get_attachment_image_src()
directly in your current code:Or you can use
wp_get_attachment_image()
in place of your current code: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.:Which will return an array of attachment objects.