2014-09-25 34 views
2

我已经在我的网站上部分成功地集成了引导图像库(http://blueimp.github.io/Bootstrap-Image-Gallery/),但是如果我在页面上发布了多个图库/文章,点击图像并且lightbox会打开所有图像每个画廊的。我想要的是灯箱只是循环点击特定画廊的图像。Bootstrap Image Gallery显示来自多个画廊的所有图像。只想显示特定的图库图像

我可以通过为每个图库硬编码HTML并设置适当的ID等来实现我想要的效果,但是我想使用Wordpress图库小部件来添加图库并使灯箱正常工作。

我在Wordpress上使用了根框架基础主题,并且玩过了gallery.php文件,但无济于事。在gallery.php文件中有一个roots_gallery函数,该函数的一部分根据图库的帖子ID创建一个div类ID,然后还为该图库指定一个唯一的编号ID。从gallery.php文件代码行是如下:

$unique = (get_query_var('page')) ? $instance . '-p' . get_query_var('page'): $instance; 
$output = '<div class="gallery gallery-' . $id . '-' . $unique . '">'; 

在同一gallery.php文件有一个roots_attachment_link_class函数,它使用str_replace功能,以改变各像册的图像的标签,如下添加缩略图类:

function roots_attachment_link_class($html) { 
    $postid = get_the_ID(); 
    $html = str_replace('<a', '<a class="thumbnail img-thumbnail"', $html); 
    return $html; 
} 
add_filter('wp_get_attachment_link', 'roots_attachment_link_class', 10, 1); 

达到我想要什么,我只需要一个data-gallery属性添加到对应于相同的画廊ID,像这样的上述str_replace功能:

$html = str_replace('<a', '<a class="thumbnail img-thumbnail" data-gallery=".blueimp-gallery-[gallery ID], $html); 

我可以用得到的画廊ID的第一部分没有问题:

$id = get_the_ID(); 

但我不能让这在roots_gallery函数创建的$unique ID部分。我曾经尝试这样做:

$id = get_the_ID(); 
    static $instance = 0; 
    $instance++; 
    $unique = (get_query_var('page')) ? $instance . '-p' . get_query_var('page'): $instance; 

    $html = str_replace('<a', '<a class="thumbnail img-thumbnail" data-gallery=".blueimp-gallery-' .$id . '-' . $unique . '"', $html); 
    return $html; 

$instance部分在roots_gallery函数中使用,但在这里它提供了一个唯一的ID对每个图像意味着灯箱展示只是一个图像。

我不是专家的PHP编码器,所以这可能是基本的东西,但我一直在挠我的头一阵子,所以任何帮助,非常感谢。如有必要,我可以提供完整的gallery.php文件。

感谢

回答

0

想想我已经破解了它,感谢cfx指向正确的方向。真的很简单,但在我有限的编码体验中,我倾向于避开全局变量,但这正是我需要的。

我在roots_gallery函数中做了$unique全局变量,然后在roots_attachment_link_class()函数中引用它。

roots_gallery

global $unique; 
$unique = (get_query_var('page')) ? $instance . '-p' . get_query_var('page'): $instance; 

roots_attachment_link_class()

global $unique; 

$html = str_replace('<a', '<a class="thumbnail img-thumbnail" data-gallery=".blueimp-gallery-' .$id . '-' . $unique . '"', $html); 
return $html; 

似乎是工作,因为我想到目前为止反正。

干杯

0

退房PHP对variable scope手册。你可能有一些运气将$instance保存到一个新的全局变量中,然后你可以在锚定标记中参照roots_attachment_link_class()

相关问题