使用 Envira Gallery 和 ACF

Using Envira Gallery and ACF

我不知道是否有人能够帮助我,但我在让 Envira Gallery 与 ACF pro 一起工作时遇到了问题。

我想做的是让 Envira Gallery 接收我用 ACF 制作的画廊。我读过的应该是可能的。目前它有点管用。我已经阅读 this 教程大约 1000 遍来检查我是否做错了什么,但我似乎无法让它工作。

我在 Envira Gallery 中创建了一个名为 New fotos 的画廊,它的 ID 为 7522,我还创建了一个名为 get_my_fotos 的 ACF 画廊字段,并在其中放置了一些图像,按照教程的指示。我从评论中的 github link 中获取了代码,因为那是一个较新的代码。

我的HTML:

<?php get_header(); ?>
    <div class="container" style="min-height:300px;margin-top:90px;">
        <?php echo do_shortcode('[envira-gallery id="7522"]'); ?>
    </div>
<?php get_footer(); ?>

现在是启用 Envira Gallery 与 ACF 一起工作的脚本:

/*
 * Populate Envira Gallery with ACF gallery field
 *
 * Filters the gallery $data and replaces with the image data for our images in the ACF gallery field.
 *
 * @uses ACF Pro
 * @uses Envira Gallery
 * @param $data
 * @param $gallery_id
 */
function envira_acf_gallery( $data, $gallery_id ) {

    // Target desired Envira gallery using ID
    if ( $data[ "id" ] == 7522 ) {

        //Setup new array to populate Envira gallery
        $newdata = array();

        // Don't lose the original gallery id and configuration
        $newdata[ "id" ] = $data[ "id" ];
        $newdata[ "config" ] = $data[ "config" ];

        if ( function_exists( 'get_field' ) )
        // Get array of images data from desired ACF gallery field
            $image_ids = get_field( 'get_my_fotos' );

        // Check to make sure array has images
        if( is_array( $image_ids ) ) {

            // Populate the Envira gallery with meta from the ACF gallery
            foreach( $image_ids as $image_id ) {
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "status" ] = 'active';    
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "src" ] = $image_id["url"];
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "title" ] = $image_id["title"];
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "link" ] = $image_id["url"];
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "alt" ] = $image_id["alt"];
                $newdata[ "gallery" ][ ( $image_id["id"] ) ][ "thumb" ] = $image_id["sizes"]["thumbnail"];
            }
        }
        // Return the new array of images
        return $newdata;
    }

}
// Add new image data to the envira gallery
add_filter( 'envira_gallery_pre_data', 'envira_acf_gallery', 10, 2);

现在,如果我检查我的检查器,我会看到:

<div id="envira-gallery-wrap-7496" class="envira-gallery-wrap envira-gallery-theme-base envira-lightbox-theme-base_dark" itemscope="" itemtype="http://schema.org/ImageGallery">
</div>

没有脚本,我看到以下内容:

<div id="envira-gallery-wrap-7522" class="envira-gallery-wrap envira-gallery-theme-base envira-lightbox-theme-base_dark" itemscope="" itemtype="http://schema.org/ImageGallery">
    </div>

所以它正在获取一些代码,但它看起来像是我删除的画廊的旧 ID。但我似乎不明白为什么它没有获取我在脚本中提供的 ID。因此没有显示图像。

有人试过这个吗?我希望这里有一些帮助。我也试过教程中的脚本,但没用。

我刚遇到同样的问题。添加以下内容解决了它:

add_filter( 'envira_gallery_get_transient_markup', '__return_false' );

有关详细信息,请参阅 How to Remove Fragment Cache from Envira

另一种解决方法是使用 Envira Gallery 提供的动态插件。如果使用短代码或经典编辑器(不适用于块编辑器),您可以将其设置为自动将默认的 WordPress 画廊显示为 Envira 画廊。然后您可以使用 ACF 画廊字段并将数据输出为 WordPress 画廊,它显示为 Envira 画廊。

关于配置 Envira 画廊的说明在 Envira Dynamic Addon page

从 ACF Gallery 文档中获取的用于显示简码的代码是:

// Load value (array of ids).
$image_ids = get_field('gallery');
if( $image_ids ) {

// Generate string of ids ("123,456,789").
$images_string = implode( ',', $image_ids );

// Generate and do shortcode.
// Note: The following string is split to simply prevent our own website from rendering the gallery shortcode.
$shortcode = sprintf( '[' . 'gallery ids="%s"]', esc_attr($images_string) );
echo do_shortcode( $shortcode );
}

我已经在本地主机网站上检查过它并且工作正常。由于它使用短代码,因此它可能不太容易受到 Envira 画廊代码更改引起的问题的影响。