How to get url of attached Image from CMB2 'file' Field type
-
I have a challenge at the moment as I write this and I honestly need your help. I’m having a field of type ‘file’ but I’m honestly failing to retrieve the url of the attached image. I’ve been to the CMB2 wiki/documentation several times and even tried a number of other solutions suggested else where but still with no luck. I’ve tried the extraction code on the demo fields in example-functions.php while adjusting the fields according ly, but still to avail. I don’t want to be so clingy and needy. But I honestly would love you to help me out on this one. I honestly don’t know what I’m doing wrong; but I can’t retrieve the url of this image I upload using the ‘file’ field type. Here’s what I have for my field;
$cmb->add_field( array( 'name' => 'Test File', 'desc' => 'Upload an image or enter an URL.', 'id' => 'featIcon1_phm1', 'type' => 'file', 'options' => array( 'url' => false, 'add_upload_file_text' => 'Add File', ), ) );
And this is how I retrieve the url from the field:
$image = wp_get_attachment_image( get_post_meta( get_the_ID(), 'featIcon1_phm1', 1 ), 'medium' ); echo $image;
There’s another problem too. I’m having this field in a metabox that only shows on a certain page template. That means that I don’t want to supply a fixed ID to the wp_get_attachment_image method because the field will be used on many pages as that share the same template. Besides, even pages that share the same template still have different IDs. I have the following parameter in my metabox array;
'show_on' => array( 'key' => 'page-template', 'value' => 'page-home-one.php' ),
Any clues?
- The topic ‘How to get url of attached Image from CMB2 'file' Field type’ is closed to new replies.