0

这是我正在尝试的网站:http: //increaseinwebtraffic.com/marywood/deals/

前几笔交易的宽度大于 620,但底部的交易量较小。我尝试使用下面的代码但没有成功。我用谷歌搜索,只找到了永久的解决方案。

<?php the_post_thumbnail( array(620,295) ); ?>

任何帮助表示赞赏。

4

2 回答 2

1

请尝试以下代码。

<?php
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
if (!empty($thumbnail_id))
{
  $thumbnail = wp_get_attachment_image_src($thumbnail_id, 'full');
  if (count ($thumbnail) >= 3)
  {
    $thumbnail_url = $thumbnail[0];
    $thumbnail_width = $thumbnail[1];
    $thumbnail_height = $thumbnail[2];

    $thumbnail_w = 620;
    $thumbnail_h = floor($thumbnail_height * $thumbnail_w / $thumbnail_width);
  }
}

if (!empty ($thumbnail_url)): ?>
  <img class="thumbnail" src="<?php echo $thumbnail_url; ?>" alt="<?php the_title_attribute(); ?>"
       width="<?php echo $thumbnail_w; ?>" height="<?php echo $thumbnail_h; ?>" />
<?php endif; ?>

http://www.boxoft.net/2011/10/display-the-wordpress-featured-image-without-stretching-it/

于 2011-10-20T14:45:37.123 回答
0

设置上面的代码然后使用 CSS 设置宽度和高度怎么样?像这样的东西:

CSS

.deals img {
 width: 620px;
 max-height: 295px;
}

HTML / PHP

<div class="deals"><?php the_post_thumbnail( array(620,295) ); ?></div>
于 2011-05-09T20:48:56.270 回答