skip to Main Content

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

Answers


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

    Login or Signup to reply.
  2. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search