2012-02-20 41 views
1

是否有可能要求Wordpress查看所有「上傳」文件夾,而不僅僅是特定的文件夾?正如你從下面的腳本看,它是尋找具有相同的名稱和特定的前綴圖片...從'上傳'的任何子文件夾調用圖像

'wp-content/uploads/2012/01/' 

我希望它是這樣的......

'wp-content/uploads/*/*/' 

爲事情是這樣的,它不工作爲別的,例如,如果圖像文件的前綴...

'wp-content/uploads/2012/02/' 

我的腳本...

$(document).ready(function(){ 


$(".gigthumb").each(function() { 
     var a = $(this).parent(); 
     a.attr("href", a.prev().attr("href")); 
    }); 



$('img.gigthumb').each(function(){ 
var foo = $(this).parent().attr('href').split('/').pop(); 
$(this).attr('src','wp-content/uploads/2012/01/' + foo + '.jpg'); 
}); 


$('img.gigthumb').error(function() { 
    $(this).attr('src','wp-content/plugins/gigpress/images/placeholder.gif'); 
}); 

希望這是有道理的。很難解釋這個問題。

回答

6

這樣做會是繁瑣和非常緩慢。你將不得不從服務器端提供所有可能的路徑,然後使用Ajax請求或其他東西來遍歷它們。這不是一個好的解決方案。

這個問題應該可以通過改變Wordpress組織上傳的方式來更容易解決。該設置是在設置 - >媒體

enter image description here

我不知道,但是,如果你這樣做在現有的安裝已經上傳會發生什麼。您可能需要重新組織/重新上傳您的所有帖子的內容才能使用它(我非常確定WP不會自動重新組織)。請自擔風險並進行備份。

+0

嗯。感謝您的回答@Pekka。看起來像一個很好的解決方案,但在這個階段可悲的是不實際。網站現在已經存在,並且有很多媒體需要重新上傳。嗯。這使我現在處於一種狀況。 – 2012-02-20 12:11:20

+0

@ Kelvin嗯,那太臭了。查看你的代碼雖然 - 我不完全明白它的作用 - 不是在Javascript中某些*點處已知的圖像的完整路徑? – 2012-02-20 12:13:41

+0

這是一個腳本,用於定製Gigpress插件並添加縮略圖。基本上,用戶上傳與URL中的帖子ID相同名稱的圖像文件(例如「an-example-event」),並且腳本將其添加到事件列表中。它在這裏很活躍[鏈接](http://www.londonirishcentre。org /即將到來的活動) – 2012-02-20 12:20:53

0

我認爲這會做到這一點。 向服務器發送請求(通過AJAX)。服務器返回到所有文件的路徑,以逗號分隔它們,一旦返回到客戶端,jQuery在逗號處分割該數據串,並將完整路徑放入數組中。完成此操作後,您可以簡單地循環訪問數組,併爲數組的每個元素創建一個圖像元素,或者執行任何您想要的操作。

的PHP文件

<?php 
function ListFiles($dir) { 

    if($dh = opendir($dir)) { 

     $files = Array(); 
     $inner_files = Array(); 

     while($file = readdir($dh)) { 
      if($file != "." && $file != ".." && $file[0] != '.') { 
       if(is_dir($dir . "/" . $file)) { 
        $inner_files = ListFiles($dir . "/" . $file); 
        if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
       } else { 
        array_push($files, $dir . "/" . $file); 
       } 
      } 
     } 

     closedir($dh); 
     return $files; 
    } 
} 


foreach (ListFiles('wp-content/uploads') as $key=>$file){ 
    echo $file .","; 
} 
?> 

來源:http://tycoontalk.freelancer.com/php-forum/41811-list-files-in-directory-sub-directories.html#post202723

的jQuery:

$(function(){ 
     $.post("getallfiles.php",{/*post data to server if needed*/}, function(data){ 
      //Splitting with comma expecting it not to be in filenames and directory names. 
      var uploads = data.split(','); 
      //uploads[0] would return the first image's path if I'm correct. 
     }) 
    }); 

我想這樣的作品,我沒有嘗試,不過,所有你需要做的是循環遍歷上傳數組,並根據圖像路徑執行任何你想要的操作。

+0

我不知道如果jQuery的分裂處理分裂時,它的字符是在最後,我猜它只會用null填充該數組的元素? – jewetnit 2012-02-20 16:15:18

相關問題