【发布时间】: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