• 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi
    have you tried ` ” target=”_blank”>Download PDF in your page template where you would like to download to occur.

    hope it helps

    sorry edit for the above should be

    “>Downloadlink here

    *not sure how to add code here sorry

    Bump. I’m running into exactly the same two issues.

    I’ve tried deactivating all other plugins to no luck. I’ve also tried older versions of CMB2 that didn’t work.

    WP v4.6
    CMB2 v2.2.2.1

    Please review the description in the wiki for the file type: https://github.com/WebDevStudios/CMB2/wiki/Field-Types#file
    You’ll see that to get the attachment ID (which is what you need to pass to wp_get_attachment_image), you need to append '_id' to your field’s meta key, so in your case, 'featIcon1_phm1_id'. The value stored to the standard meta key ('featIcon1_phm1') is the attachment URL.

    Justin, thank you for the explanation. It worked perfectly.

    I’m following up on the second part of the original post:

    . 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.

    What I found after about an hour of playing around is that CMB2 will not recognize templates outside of the main theme directory. I use FoundationPress (https://github.com/olefredrik/FoundationPress) as a base theme, which organizes page templates into a sub-folder of the main theme. When my page templates are stored in this folder, CMB2 won’t recognize them.

    To solve my problem I moved my templates into the main theme, then I deactivated and reactivated CMB2 and the “show_on” filter worked perfectly.

    CMB2 simply checks if the ‘show_on’ value matches the meta value for ‘_wp_page_template’. If it wasn’t working before moving it out of a directory, that means the ‘show_on’ value you were using was wrong. Likely you were using <template-file-name>.php when it should have been <templates-dir>/<template-file-name>.php. If that’s not the case, it’s possible it’s a bug, but at first glance over the code, it appears it’s simply a meta comparison.

    Confirmed. Thank you for the explanation Justin.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get url of attached Image from CMB2 'file' Field type’ is closed to new replies.