Archive show thumbnail

时间:2012-07-14 作者:David Garcia

我想在存档页面中的帖子旁边显示缩略图,但是只有在帖子有特色图片时才会显示缩略图。

所以我想显示附在帖子上的图像的缩略图,但我不知道如何显示。

目前,我正在使用以下代码显示缩略图(如果设置为特色)。

<?php if ( has_post_thumbnail()) : ?>
   <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
   <?php the_post_thumbnail(thumbnail, array(\'class\' => \'alignleft\')); ?>
   </a>
<?php endif; ?>
欢迎提出任何建议

1 个回复
SO网友:Bainternet

您可以检查帖子是否为缩略图,如果不是缩略图,则获取帖子中的第一幅图像:

<?php
$size = \'thumbnail\';
if ( has_post_thumbnail() ) {
    ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php the_post_thumbnail($size, array(\'class\' => \'alignleft\')); ?>

   <?php
} else {
    $attachments = get_children( array(
        \'post_parent\' => get_the_ID(),
        \'post_status\' => \'inherit\',
        \'post_type\' => \'attachment\',
        \'post_mime_type\' => \'image\',
        \'order\' => \'ASC\',
        \'orderby\' => \'menu_order ID\',
        \'numberposts\' => 1)
    );
    foreach ( $attachments as $thumb_id => $attachment ){ //this was missing
        ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
        <?php echo wp_get_attachment_image($thumb_id, $size); ?>
        </a>
        ?>
    }
}
?>

结束

相关推荐