如何在wordpress中“动态”更改图像的大小

时间:2014-04-30 04:17:53

标签: php wordpress

我正在开发一个自定义Wordpress网站,我需要能够输出分配给帖子的图像作为"特色图片"所以,这是我的PHP代码

$theimage=wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID(), array( 160,260 ), false, '' ), 'product-image2');    
$product_shortcode .= '<li>';
$product_shortcode .= '<a href="' . get_permalink() . '"><h4>'. get_the_title().'</h4>';
$product_shortcode .= '<img src="'.$theimage[0].'" alt="" /> ';
$product_shortcode .= '</a>';
$product_shortcode .= '</li>';

现在这是我的问题 enter image description here 正如您所见,图像以(160 x 140)显示。我需要完整显示图像。当我检查它时,输出中的图像src是../~image-path/feed160x140.png。哪个是wordpress生成的缩略图大小。如果它显示正常../~image-path/feed.png我会没事的。

在我的代码中,根据Wordpress Reference Website数组(160X260)应该在那些维度中显示它。没有发生在我身上。

我需要WP大师的帮助。

2 个答案:

答案 0 :(得分:2)

我删除了第一个答案。我错过了很明显的事情。

您使用的get_post_thumbnail_id()仅将帖子ID作为参数,但您传递get_the_post_thumbnail()的参数。您还将不存在的参数传递给get_the_post_thumbnail

而不是使用

get_post_thumbnail_id(get_the_ID(), array( 160,260 ), false, '' )

你应该使用

get_the_post_thumbnail($post_id, array(160,260) );

答案 1 :(得分:2)

试试这个,我希望这对你有用。

$theimage=wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()),'full');    
$product_shortcode .= '<li>';
$product_shortcode .= '<a href="' . get_permalink() . '"><h4>'. get_the_title().'</h4>';
$product_shortcode .= '<img class="img-responsive" src="'.$theimage[0].'" alt="" /> ';
$product_shortcode .= '</a>';

$product_shortcode .= '</li>';

如果您使用bootstrap 3其他明智的写css,则直接使用img-responsive这个类。

.img-responsive{
  height: 180px;
  width: 100%;
  display: block;
}