Reputation: 311
I made a image add file field in by cmb2 metabox like
$FF_side_settings->add_field( array(
'name' => 'Upload background image',
'desc' => 'Upload an image or enter an URL.',
'id' => 'ff_image_upload',
'type' => 'file',
// Optional:
'options' => array(
'url' => true,
'add_upload_file_text' => 'Add image'
),
) );
I want to get the image location and show the value in my div image background-url
<div class="fun-bg " style='background-image: url("'.$image.'");'>
I used wp_get_attachment_image( get_post_meta( get_the_ID(), 'ff_image_upload', 1 ), 'medium' );
for that but i faild to do that. can someone please help me to find out where is my problem please and fix that............
Upvotes: 3
Views: 2273
Reputation: 1477
OK wp_get_attachment_image()
returns image tag:
<img src="image_src" ... />
in your case you need echo function return
<div class="fun-bg">
<?php echo wp_get_attachment_image( get_post_meta( get_the_ID(), 'ff_image_upload', 1 ), 'medium' ); ?>
</div>
or get only src attribute, not image tag himself.
EDIT: I installed this plugin and did some tests with images metabox.
So when you add image through this plugin it creates two records in postmeta table. First is with metakey ff_image_upload_id
with value id of the post (52 in my case), and second is with key ff_image_upload
and value = image_path.
get_post_meta( get_the_ID(), 'ff_image_upload', 1 )
will return image path - http://host/site-name/wp-content/uploads/year/month/name.jpg
This is path to the original image, not 'medium', 'thumbnail' or something like
get_post_meta( get_the_ID(), 'ff_image_upload_id', 1 )
will return post_id (52).
If you want to use wp_get_attachment_image()
to display image directly, you must append the post id with id
so you must use it like this:
<?php echo wp_get_attachment_image( get_post_meta( get_the_ID(), 'ff_image_upload_id', 1 ), 'medium' ); ?>
If you want the url
of the image, this will return the url
of the medium
image:
<?php wp_get_attachment_image_src( get_post_meta( get_the_ID(), 'ff_image_upload_id', 1 ), 'medium' )[0]; ?>
Upvotes: 3