Wordpress - 自定义图像大小不起作用

时间:2013-11-22 21:32:49

标签: php wordpress image-resizing

我在使用自定义图片尺寸方面遇到了麻烦。

我在function.php中有这个:

//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
$keys = array_reverse(array_keys($files));
$j=0;
$num = $keys[$j];
$image=wp_get_attachment_image($num, 'test', true);
$imagepieces = explode('"', $image);
$imagepath = $imagepieces[1];
$main=wp_get_attachment_url($num);
    $template=get_template_directory();
    $the_title=get_the_title();
print "<img src='$main' alt='$the_title' />";
endif;
}

add_image_size('test', 145, 145, true);

这样做会检查帖子的缩略图,如果没有,它会搜索帖子中的第一张图片。到目前为止一切都那么好,但我不能让它显示我想要的图像尺寸。

wp_get_attachment_image($ num,'test',true);应显示上传时创建的“测试”大小(我还运行了重新生成的缩略图,并且所有图像都已调整大小)。但无论我输入什么而不是测试(完整,中等,缩略图等),代码总是调用完整的图像(它被压缩到145x145大小而不是显示裁剪的缩略图)。

代码来自http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/(其旧代码,因此WP中的某些更改可能会导致此操作失败)。

这是我完成网站所需做的最后一件事。

0 个答案:

没有答案