Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

wordpress - foogallery php shortcode with ID taken from AFC custom field variable does no display

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"

question from:https://stackoverflow.com/questions/65892204/foogallery-php-shortcode-with-id-taken-from-afc-custom-field-variable-does-no-di

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...