从高级自定义字段编辑器获取图库图像

get gallery images from advanced custom field Editor

我使用 advanced custom field 插件

创建了一个带有编辑器的元框

这个元框显示了一个文本编辑器,我可以从中创建图库并将其添加到 post。

但是在检索图库数据时。

$gallery = get_post_meta($post->ID, 'gallery', true);     
echo $gallery;

这显示

[gallery ids="53,54,55,56"]

一般来说,图库数据可以像

一样检索
$galleries = get_post_gallery_images( get_the_ID() ); 

但是如果我使用元框保存画廊,这似乎不起作用。

是否有 wordpress 方法来获取图库图像并循环浏览它们并在灯箱中显示这些图像,或者我应该尝试任何其他方法吗?

1st 我假设您已将“[gallery ids="53,54,55,56"]”这个短代码放在元文件下,为什么

$gallery = get_post_meta($post->ID, 'gallery', true);
echo $gallery;

Return

[gallery ids="53,54,55,56"]

可以试试这个

$gallery = get_post_meta($post->ID, 'gallery', true);
echo do_shortcode($gallery);

$galleries = get_post_gallery_images( get_the_ID() );

Returns 包含图片上传源的图片项目列表。

成功了

<?php
$gallery = get_post_meta($post->ID, 'gallery', true);

preg_match('/\[gallery.*ids=.(.*).\]/', $gallery, $ids);
$images_id = explode(",", $ids[1]);
if ($images_id[0] != "") {
    if (is_array($images_id) || is_object($images_id)) {
        foreach ($images_id as $image) {
            $image_url = wp_get_attachment_image_src($image, 'banner');
            ?>
            <a href="<?php echo $image_url[0]; ?>">
                <?php echo wp_get_attachment_image($image, 'destinatoin', 'false', array("class" => "img-responsive")); ?>
            </a>
        <?php }
    }
} ?>

应该做的是

preg_match('/\[gallery.*ids=.(.*).\]/', $gallery, $ids);
$images_id = explode(",", $ids[1]);

这将创建一个 ID 数组,然后我可以通过

遍历图像
   foreach ($images_id as $image) {
        $image_url = wp_get_attachment_image_src($image, 'banner');
        ?>
        <a href="<?php echo $image_url[0]; ?>">
            <?php echo wp_get_attachment_image($image, 'destinatoin', 'false', array("class" => "img-responsive")); ?>
        </a>
    <?php }

不可能,因为我认为您没有将图库短代码插入到帖子编辑器中, 所以 $galleries = get_post_gallery_images( get_the_ID() );空手而来

所以在你的 meta box filed 中只输入图像 ID, 喜欢

"53,54,55,56"

$gallery = get_post_meta($post->ID, 'gallery', true);

$gallery 变量将 return 逗号分隔的字符串,然后将其展开 $gallery_image_array = explode($画廊);

那么$gallery_image_array会有一个图片id,所以你可以循环遍历它,你可以通过wp_get_attachment_image_src

得到这些图片

随心所欲打印 :)