2014-09-25 38 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()

相關問題