Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Loop with images from post to

Tags:

wordpress

I made an Wordpress theme, with pages and posts. The loop of posts show me a short brief of post and a Continue reading link. I like this, but how can I make the theme show in the post brief of the loop image(s) attached to post at beginning, if any.

Thank you!

like image 555
Adrian Florescu Avatar asked Aug 31 '25 20:08

Adrian Florescu


1 Answers

You can get your attached images by using:

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'numberposts' => 1,
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post_parent' => $post->ID
);
$images = get_posts($args);

and display it like this:

echo wp_get_attachment_image($images[0]->ID, $size='attached-image');
like image 90
ariefbayu Avatar answered Sep 03 '25 18:09

ariefbayu