I'm developing a WordPress theme and using custom fields. I'm facing the following problem

100 Views Asked by At

My code

 <`li><img src="<?php echo get_post_meta($post->ID, '_for-gallery', true); ?> " alt=""></li>`

The code should give this:-

 <img src="Array" alt="" draggable="false">

But the code is giving this:-

<img src=" " alt="" draggable="false">
2

There are 2 best solutions below

0
On

When you use get_post_meta function with $single as true just get the first element, but false get all elements separated by comma.

Looking your code, i suppose that custom_field is an url, in that case, why you need get an array as a url? you must get a string...

You can check here: https://developer.wordpress.org/reference/functions/get_post_meta/

2
On

Look at the code reference for get_post_meta() - it says that the function will return an array if the 3rd parameter ($single) is false:

Return: (mixed) An array if $single is false. The value of the meta field if $single is true. False for an invalid $post_id.

In your code you're sending true, try this instead:

get_post_meta($post->ID, '_for-gallery', false);

More info here: https://developer.wordpress.org/reference/functions/get_post_meta/