2

I have a "cmb2" custom field with type->file. and i use it to upload images.

If i use:

echo get_post_meta( $post->ID, '_pf_photo1', 'medium' );

i get the url of the full image (not the medium one).

How can i get the url of the 'medium' / 'thumbnail' and so on...

2 Answers 2

2

The get_post_meta() function can help to get the meta field but will not retrieve different size.

Assuming _pf_photo1 embed the attachment id, you can do something like that:

// Note the "_id" suffix
$attachment_id = get_post_meta($post->ID, '_pf_photo1_id', true);

Last parameter for this function can not be 'medium',

Now,you can use $attachment_id with different function depending on what you really want to get (url, img element...):

$attachment_element = wp_get_attachment_image( $attachment_id, 'medium' );
echo $attachment_element;

There is more ways to get details for attachment wp_get_attachment_url(), wp_get_attachment_image_src (that returns an array with url, width, height).

You will find more details to discover these functions here

Hope it helps !

3
  • $attachment_id = get_post_meta($post->ID, '_pf_photo1', true); doesn't give me the "id". if i do echo $attachment_id; i get the full image url.
    – Ben
    Nov 14, 2016 at 10:31
  • So, maybe, you will find an answer with this other question : wordpress.stackexchange.com/questions/39387/… . Note that to work with image size easily, you maybe need to consider to save the attachment id instead of the link.
    – Benoti
    Nov 14, 2016 at 10:36
  • 1
    It seems that in "cmb" you have to add "_id" to the field key to get the id so it's: get_post_meta($post->ID, '_pf_photo1_id', true); now it works. Thanks!
    – Ben
    Nov 14, 2016 at 10:45
3

You can get the ID of the file with _pf_photo1_id, then it's easy to get any size URL:

$file_id = get_post_meta( $post->ID, '_pf_photo1_id', true );

if ( $file_id )
    echo wp_get_attachment_image_url( $file_id, 'medium' );
7
  • $file_id = get_post_meta( $post->ID, '_pf_photo1_id', true ); doesn't give me the "id". if i do echo $file_id; i get the full image url.
    – Ben
    Nov 14, 2016 at 10:31
  • Are you sure? The documentation describes the _id suffix Nov 14, 2016 at 10:38
  • It seems that in "cmb" you have to add "_id" to the field key to get the id so it's: get_post_meta($post->ID, '_pf_photo1_id', true); now it works. Thanks!
    – Ben
    Nov 14, 2016 at 10:45
  • @Ben Are you serious? That is literally what my answer was. And you accepted the other answer that doesn't even explain this. Nov 14, 2016 at 10:47
  • Sorry, i didn't get you, but anyway i accepted the other answer because it's more detailed about the main subject of the question. did give your answer a plus one... sorry.
    – Ben
    Nov 14, 2016 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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