0

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 Answers2

0

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/

Hillel
  • 811
  • 2
  • 7
  • 19
  • Thanks for the replay. But when I send "false" it shows:- `` – Sadman Sorowar Jun 13 '21 at 08:13
  • That's a good thing! it means that you do get an array response that way :) If you want to see what's inside, try: $post_meta = get_post_meta($post->ID, '_for-gallery', false); print_r($post_meta); – Hillel Jun 13 '21 at 08:23
0

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/