I have ACF custom field in posts with the gallery ID. The custom field is stored in wp_postmeta table.
I am trying to execute shortcode on post page with the gallery id assigned to this post.
my code:
$post = get_post(); $gallery_id = the_field('gallery', $post->ID ); echo do_shortcode('[foogallery id="'. $gallery_id .'"]');
returns “The gallery was not found!”
echo($gallery_id); // returns 19557 echo do_shortcode('[foogallery id="19557"]'); // works well
How to execute the shortcode ont the post page with the ACF value for this post?
I was trying get_field() also but when echoing it returned: “Array to string conversion”
Advertisement
Answer
Try this:
$gallery_id = get_field('gallery', $post->ID );
the_field()
(docs) is for directly outputting a value, while get_field()
(docs) is for getting the value and for example setting a variable with it.
Edit: I misread your question and saw you already tried this. In that case, try var_dump($gallery_id)
, look for the returned values, and use the correct array key in returning the gallery ID.
So if the array key is key
, you’d use $gallery_id['key']
to output this key.