开发者

Wordpress - Loop with images from post to

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!


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');


This for getting all attachement images with your post.

   $args = array( 
                 'post_type' => 'attachment', 
                 'post_mime_type' => 'image',
                 'post_status' => null, 
                 'post_parent' => $post->ID 
           );

   $attachments = get_posts( $args );

   if ($attachments) {
      foreach ( $attachments as $post ) {
         $img = wp_get_attachment_image_src($post->ID, 'medium'); 
         $fullsize = wp_get_attachment_image_src($post->ID, 'full');
      }
   }


You should add in your loop:

<?php
   if(has_post_thumbnail()) {
      $theimage = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'thumbnail' );
}
?>
<img class="img_class" src="<?php echo $theimage[0]; ?>" />

Where "thumbnail" correspond to the size you want to show.

Remember that there is also a WordPress specific site in StackExchange

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜