WordPress – custom image sizes not working

I am having trouble with using custom image size.

I have this in function.php:

Read More
//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);

So what this does it check for the post’s thumbnail, and if there is none, it searches to the first image in a post. So far so good, but I can’t get it to display the image size that I want.

wp_get_attachment_image($num, ‘test’, true); should be displaying the ‘test’ size, which is created when uploaded (I also ran regenerate thumbnails, and all the images have been resized). But no matter what I enter instead of test (full, medium, thumbnail, etc), the code always call the full image (which gets squished to the 145×145 size instead of displaying the cropped thumbnail).

The code is from http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/ (its old code, so probably some change in WP is making this fail).

Its the last thing I need to do to finish a site.

Related posts

Leave a Reply