1

Code in my funcitons.php

function custom_thumbs() {
    add_image_size( 'cover-size', 1600, 450, true ); 
}
add_action( 'after_setup_theme', 'custom_thumbs' );

Usage:

echo the_post_thumbnail($post->ID,'cover-size');

but this is the result:

<img width="3648" height="2736" src="http://localhost/sonru/wp-content/uploads/2015/12/DSC04656.jpg" class="attachment-9 size-9 wp-post-image" alt="DSC04656" cover-size="" srcset="http://localhost/sonru/wp-content/uploads/2015/12/DSC04656-300x225.jpg 300w, http://localhost/sonru/wp-content/uploads/2015/12/DSC04656-600x450.jpg 600w" sizes="(max-width: 3648px) 100vw, 3648px">

What Am I missing here?

-Edit-

I'm already regenerating thumbnails using the regenerate thunbnails plugin

2
  • As a side-note, your comment next to add_image_size suggests there would be an unlimited height which is not the case as you've specified a max-height of 450 ( and anything else to be cropped ). If you truly want to replicate an "unlimited height" you would need to put in a extremely large number such as 999999
    – Howdy_McGee
    Dec 29, 2015 at 15:32
  • 1
    @Howdy_McGee copy and past has a danger.... edited ;) Dec 29, 2015 at 15:33

2 Answers 2

4

You may need to regenerate your images. There are plugins to accomplish this or re-upload your image and see if the size doesn't take affect. It won't happen automatically to existing files.

First, check your uploads directory to see if the files exists that way you know they are being generated. Then try another function to output the results.

What do you end up with when you use:

$attachment_id = get_post_thumbnail_id( $post->ID ); 
$img_src = wp_get_attachment_image_url( $attachment_id, 'cover-size' ); 
$img_srcset = wp_get_attachment_image_srcset( $attachment_id, 'cover-size' );

Also if may be handy to output your meta data for that attachment - specifically to see the size information:

print_r ( wp_get_attachment_metadata( $attachment_id) );

The above wp_get_attachment_image_srcset requires WordPress 4.4 and you can find more information about Responsive Images in WordPress 4.4.

Assuming the files exist:

Are you in a Loop or outside of it?

If you're inside try the_post_thumbnail ('cover-size');

otherwise echo get_the_post_thumbnail ($post,'cover-size');

4
  • Forgot to mention, I'm already using regenerte thumbnails plugin Dec 29, 2015 at 15:16
  • Yes, actually DSC04656-1600x450.jpg exists... Dec 29, 2015 at 15:22
  • 1
    wp_get_attachment_image_url works, but wp_get_attachment_image_srcset returns empty... ( If you edit the quesiton i'll acept it) Dec 29, 2015 at 15:37
  • You are just copying info from my answer now and I don't appreciate it. Your edits are invalid Dec 29, 2015 at 16:01
1

You are using the wrong function:

  • the_post_thumbnail() does not accept the post ID as value, but the image size.

  • get_the_post_thumbnail() is what you need and maybe trying to use. The first parameter accept the post ID and the second parameter accepts the image size to use as in your question

SOLUTION

Swap the_post_thumbnail() with get_the_post_thumbnail()

EDIT

Just a note, make sure that you add your new image size after you have added support for thumbnails. Also, you would want to add a new post thumbnail size with set_post_thumbnail_size()

1
  • See my edit in EDIT and don't forget to regenerate thumbnails Dec 29, 2015 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.