How to Display CMB2 File and File List Images on Your WordPress Frontend
Content
A common challenge when using the CMB2 plugin for WordPress is getting uploaded images to display correctly on the frontend of your website. Based on community discussions, this issue often stems from a misunderstanding of how CMB2 stores file data and which WordPress functions to use for retrieval.
The Core of the Problem: URL vs. ID
CMB2's 'file' and 'file_list' field types store data in a specific way that is not always intuitive. By default, a 'file' field saves the image's URL in the database. However, it also automatically saves the attachment ID in a separate meta key. This dual-saving mechanism is powerful but can lead to confusion.
Many developers try to use the URL with functions like wp_get_attachment_image(), which expects an attachment ID, not a URL. This mismatch is the most frequent cause of images not displaying, resulting in broken image links or empty variables.
How to Properly Retrieve and Display Images
Solution 1: For the 'file' Field Type
To display an image from a standard 'file' field, you must retrieve the attachment ID, not the URL. CMB2 saves the ID in your field's ID with _id appended.
Example Code:
$image_id = get_post_meta( get_the_ID(), 'your_field_id_id', true );
if ( $image_id ) {
echo wp_get_attachment_image( $image_id, 'medium' );
}
In this example, if your field ID is wiki_test_image, the attachment ID is stored in wiki_test_image_id. Using the ID allows you to leverage WordPress functions like wp_get_attachment_image() to output the image tag with a specific size (e.g., 'medium', 'large', or your own custom size).
Solution 2: For the 'file_list' Field Type
The 'file_list' field stores an array of items where the attachment ID is the key and the URL is the value. To display a gallery of images, you need to loop through this array.
Example Code:
$files = get_post_meta( get_the_ID(), 'your_file_list_id', true );
if ( $files ) {
foreach ( (array) $files as $attachment_id => $file_url ) {
echo wp_get_attachment_image( $attachment_id, 'thumbnail' );
}
}
Why This Approach is Better
Storing and using the attachment ID is the recommended method for several reasons:
- Image Sizes: It allows you to easily generate any registered image size (thumbnail, medium, large) without having to manually create and manage them.
- Metadata: You can access other attachment metadata, such as the title, alt text, or caption, using functions like
get_the_title( $attachment_id ). - Site Migration: Using IDs is more robust than storing absolute URLs. If your site domain changes, the IDs remain correct, while hardcoded URLs will break. Serialized data containing URLs must be handled carefully during migration to avoid corruption.
What to Do If Your Images Are Still Missing
If you've confirmed your code is correct but images are not showing, try these troubleshooting steps:
- Check the Saved Data: Use
var_dump( get_post_meta( get_the_ID(), 'your_field_id', true ) );to see exactly what is stored in the database. This will tell you if you are getting an array, an ID, a URL, or nothing at all. - Verify the Meta Key: Ensure the meta key you are using in your
get_post_meta()call matches the field ID defined in your CMB2 configuration exactly. - Review CMB2 Documentation: The CMB2 team maintains extensive documentation on GitHub. The file field type documentation provides essential examples for output.
By understanding how CMB2 handles file data and using the appropriate WordPress functions, you can reliably display images and create dynamic, professional-looking frontend displays.
Related Support Threads Support
-
img link in post meta vs. just the img attachment IDhttps://wordpress.org/support/topic/img-link-in-post-meta-vs-just-the-img-attachment-id/
-
Media Modal issue when opened using type file_listhttps://wordpress.org/support/topic/media-modal-issue-when-opened-using-type-file_list/
-
Display image caption/title/description with File list imageshttps://wordpress.org/support/topic/display-image-captiontitledescription-with-file-list-images/
-
oEmbed questionhttps://wordpress.org/support/topic/oembed-question/
-
Gallery data (file_list) unavailable/empty after database migrationhttps://wordpress.org/support/topic/gallery-data-file_list-unavailable-empty-after-database-migration/
-
Blank Gallery Imageshttps://wordpress.org/support/topic/blank-gallery-images/
-
Images not saving in file_list field typehttps://wordpress.org/support/topic/images-not-saving-in-file_list-field-type/
-
file_list data not stored when WooCommerce HPOS activehttps://wordpress.org/support/topic/file_list-data-not-stored-when-woocommerce-hpos-active/
-
Retrieving categories from imageshttps://wordpress.org/support/topic/retrieving-categories-from-images/
-
Images from “file” field nowhere to be seenhttps://wordpress.org/support/topic/images-from-file-field-nowhere-to-be-seen/
-
File list (file upload) from the front end using WP REST APIhttps://wordpress.org/support/topic/file-list-file-upload-from-the-front-end-using-wp-rest-api/
-
Get file title attributehttps://wordpress.org/support/topic/get-file-title-attribute/
-
Unable to display image urlhttps://wordpress.org/support/topic/unable-to-display-image-url/
-
Output image with my sizehttps://wordpress.org/support/topic/output-image-with-my-size/
-
User meta – Avatar – return smaller sizehttps://wordpress.org/support/topic/user-meta-avatar-return-smaller-size/
-
Field file show no imagehttps://wordpress.org/support/topic/field-file-show-no-image/
-
Get url of all items in file listhttps://wordpress.org/support/topic/get-url-of-all-items-in-file-list/
-
Repeating Field for Sliderhttps://wordpress.org/support/topic/repeating-field-for-slider/
-
cmb2 field type : file is not displaying image on front end?https://wordpress.org/support/topic/cmb2-field-type-file-is-not-displaying-image-on-front-end-2/
-
Image field (ACF) to File field (CMB2)https://wordpress.org/support/topic/image-field-acf-to-file-field-cmb2/
-
cmb2 field type : file is not displaying image on front end?https://wordpress.org/support/topic/cmb2-field-type-file-is-not-displaying-image-on-front-end/
-
trying to display images into modalhttps://wordpress.org/support/topic/trying-to-display-images-into-modal/
-
File List reordering for wp-json outputhttps://wordpress.org/support/topic/file-list-reordering-for-wp-json-output/
-
File list of option page do not display in frontendhttps://wordpress.org/support/topic/file-list-of-option-page-do-not-display-in-frontend/
-
File List displayhttps://wordpress.org/support/topic/file-list-display/
-
Loading Attachments into file_listhttps://wordpress.org/support/topic/loading-attachments-into-file_list/
-
CMB2 Rest API – image/file field as an objecthttps://wordpress.org/support/topic/cmb2-rest-api-image-file-field-as-an-object/
-
echo the "file" meta box problemhttps://wordpress.org/support/topic/echo-the-file-meta-box-problem/
-
Display the Metadata questionhttps://wordpress.org/support/topic/display-the-metadata-question/
-
how to get image url from file_listhttps://wordpress.org/support/topic/how-to-get-image-url-from-file_list/
-
Save or retrieve attachment id instead of attachment urlhttps://wordpress.org/support/topic/save-or-retrieve-attachment-id-instead-of-attachment-url/
-
Image size problemhttps://wordpress.org/support/topic/image-size-problem-10/
-
(file_list) count items(images)https://wordpress.org/support/topic/file_list-count/
-
Missing image values when I set my site livehttps://wordpress.org/support/topic/missing-image-values-when-i-set-my-site-live/
-
How to display imageshttps://wordpress.org/support/topic/how-to-display-images-3/
-
CMB2 file_list issue ??https://wordpress.org/support/topic/cmb2-file_list-issue/
-
Theme Customizer Bug – Featured Imagehttps://wordpress.org/support/topic/theme-customizer-bug-featured-image/
-
programmatically insert data into file_listhttps://wordpress.org/support/topic/programmatically-insert-data-into-file_list/