从wordpress帖子中获取完整尺寸的图片

时间:2018-07-23 00:50:07

标签: php wordpress

我具有此功能,该功能可以获取postID并返回帖子的缩略图

function stack_309224_attachhment($atts){
extract(shortcode_atts( array(
  'id'      => 1, // default id of the post
  "max"   =>4 ,// default max number of images to display
  "featured"=>"false" // if to retrieve the post featured image or not, default false
),  $atts ));

ob_start();
if($atts['featured']=="true"){ // display only the featured image  get_the_post_thumbnail get_post_thumbnail_id
  ?>
  <figure><img src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id( (int)$atts['id']) )[0]?>" /></figure>  



  <?php
}
else{ // display a gallery of attached images within  "max" attr.
  $images=get_attached_media( 'image', (int)$atts['id'] );
  if(count($images) > 0 ){
   ?>
   <div class="gallery">
   <?php
   $index=0;
   foreach($images as $image){
     if($index < (int)$atts['max']){ //display image only if within max 
    ?>
    <figure><img src="<?php echo wp_get_attachment_url($image->ID)?>" /></figure>
    <?php
      $index ++;
     }
   }
 ?></div><?php
 }     
}
return ob_get_clean();
}

但是问题是,它返回的是缩略图的最小尺寸 例如“ http://Mysite/my_image_n-150x150.jpg

如何获取完整尺寸的图像?例如“ http://Mysite/my_image_n.jpg

1 个答案:

答案 0 :(得分:1)

您可以使用此功能

<?php     
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full', false );

“完整”是图片的大小

根据您的情况,您可以更改为

 <figure><img src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id( (int)$atts['id']), 'full', false )[0]?>" /></figure>