我在posts中有ACF自定义字段和图片库ID,自定义字段存储在wp_postmeta表中。
我正试图在post页面上使用分配给此帖子的图库id来执行短代码。
我的代码:
$post = get_post();
$gallery_id = the_field('gallery', $post->ID );
echo do_shortcode('[foogallery id="'. $gallery_id .'"]');返回“没有找到画廊!”
echo($gallery_id); // returns 19557
echo do_shortcode('[foogallery id="19557"]'); // works well如何使用此帖子的ACF值在post页面上执行短代码?
我也尝试过get_field(),但是当回显它时:“数组到字符串转换”
发布于 2021-01-25 21:04:17
发布于 2021-01-26 14:55:24
根据您的其他注释,它看起来像是the_field() (文档)返回一个数组,所以如果您总是期待一个数组,您可以使用reset() (文档)返回该数组中的第一个值。
您还可以使用内置函数foogallery_render_gallery而不是do_shortcode。
在调用函数之前检查这些函数是否存在,这是一个很好的实践。这将有助于当这些插件暂时停用,然后您将避免致命的错误。
试着做这样的事情:
//get the current post
$post = get_post();
//check to make sure ACF plugin is activated
if ( function_exists( 'the_field' ) ) {
//get the field value from ACF
$gallery_id = the_field( 'gallery', $post->ID );
//we are expecting an array, so use reset to rewind the pointer to the first value
reset( $gallery_id );
//check to make sure FooGallery is activated
if ( function_exists( 'foogallery_render_gallery') ) {
//finally, render the gallery
foogallery_render_gallery( $gallery_id );
}
}https://stackoverflow.com/questions/65892204
复制相似问题