WordPress 获取文章的缩略图代码

当设置的“特色图”的时候调用特色图,如果没有则调用文章的第一个图片,如果还没有就调用主题默认图片。

<a href="<?php the_permalink(); ?>">
    <?php 
      if (has_post_thumbnail()) { 
        the_post_thumbnail( 'thumbnail'); 
      }else{ 
        $args=array( 'post_type'=>'attachment','numberposts' => 1,'post_status' => null,'post_parent' => $post->ID); 
        $attachments = get_posts( $args ); 
        if($attachments){ 
          foreach($attachments as $attachment){ 
            $src = (array) wp_get_attachment_image_src( $attachment->ID, 'thumbnail');
            if($src){ ?>
              <img src="<?php echo $src[0]; ?>" height="280" width="300" />
            <?php 
            } 
          } 
        }else{ ?>
          <img src="<?php echo get_template_directory_uri(); ?>/images/default_thumbnail.png" height="280" width="300" />
          <?php 
        } 
      } 
    ?>
</a>