How to get shortcode thumbnail size?

my code get shortcode thumbnail size in functions:

function thumb_medium( $atts, $content = null ) {
    return wp_get_attachment_url( get_post_thumbnail_id( $post_id, 'medium') );
//or wp_get_attachment_url( get_post_thumbnail_id( $post_id, 'large') );
//or wp_get_attachment_url( get_post_thumbnail_id( $post_id, array(100,100)) );
add_shortcode("get_urlthumb", "thumb_medium");

but [get_urlthumb] always echo full url thumbnail as: wp-content/uploads/2015/08/origin-image-upload.jpg



I think your code is very close, try to use wp_get_attachment_image_src() instead:

function thumb_medium( $atts, $content = null ) {
    // return wp_get_attachment_url( get_post_thumbnail_id( $post_id, 'medium') );
    global $post;
    $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' )[0];
    if ($thumb_url) {
        return $thumb_url;
add_shortcode("get_urlthumb", "thumb_medium");
  • return wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium' )[0]; that is error code. checking pls.
    – DinhTv
    Aug 24 '15 at 4:16
  • Don't you need a global $post?
    – Sisir
    Aug 24 '15 at 4:21
  • @DinhTv this answer already in the right direction. Did you try it? I have added link to the codex page on this answer. Kindly read the codex.
    – Sisir
    Aug 24 '15 at 4:33
  • @DinhTv are you getting an error message? Can you give us more information on what is happening when you use the shortcode? @Sisir I edited the answer to load the global $post variable and use a conditional to prevent returning a null value for posts/pages lacking a Featured Image
    – Kory
    Aug 24 '15 at 4:45
  • @Sisir oh, so sorry.That is correct Answers. Thanks $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); $thumb_url = $thumb_url[0];
    – DinhTv
    Aug 24 '15 at 6:03

You can also go with aq_resize function.
There are second and third arguments are given for size, so you can give any size that's you want.
Even you can also set these size using shortcode. Check how to pass arguments to shortcode and pass using shortcode if want to fetch dynamic size for each time

function thumb_medium( $atts, $content = null ) {
    $thumb = wp_get_attachment_url( get_post_thumbnail_id( $post_id, 'full') );
    $thumb = aq_resize( $thumb, 400, 400, true ); //resize & crop img
add_shortcode("get_urlthumb", "thumb_medium");

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.