【问题标题】:Using Envira Gallery and ACF使用 Envira Gallery 和 ACF
【发布时间】:2018-03-22 11:28:26
【问题描述】:

我不知道是否有人可以帮助我,但我无法让 Envira Gallery 与 ACF pro 合作。

我想做的是让 Envira Gallery 提取我用 ACF 创建的画廊。我读过的应该是可能的。现在它有点工作。我已经阅读了大约 1000 次 this 教程来检查我是否做错了什么,但我似乎无法让它发挥作用。

我在 Envira Gallery 中创建了一个名为 New fotos 的画廊,它的 ID 为 7522,我还创建了一个名为 get_my_fotos 的 ACF 画廊字段,并按照教程的说明将一些图像放入其中。我已经从 cmets 的 github 链接中获取了代码,因为那是一个较新的代码。

我的 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。因此不显示任何图像。

以前有人试过吗?我希望这里有一些帮助。我也尝试了教程本身中的脚本,但没有奏效。

【问题讨论】:

    标签: wordpress advanced-custom-fields image-gallery


    【解决方案1】:

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

    add_filter( 'envira_gallery_get_transient_markup', '__return_false' );
    

    请参阅How to Remove Fragment Cache from Envira 了解更多信息。

    【讨论】:

    • 嗨,我使用了一个非常相似的代码,但我发现灯箱总是在以这种方式生成的画廊的第一张图像上打开。有什么想法吗?
    【解决方案2】:

    解决此问题的另一种方法是使用 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 );
    }
    

    我已经在 localhost 网站上检查了它并且工作正常。由于它使用简码,因此可能不太容易受到 Envira 库代码更改引起的问题的影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多