Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP DirectoryIterator加载更多按钮?_Php_Ajax_Wordpress - Fatal编程技术网

用PHP DirectoryIterator加载更多按钮?

用PHP DirectoryIterator加载更多按钮?,php,ajax,wordpress,Php,Ajax,Wordpress,我在我的WordPress网站上有很多“图像库”样式的页面。我正试图找出是否有可能,使用我正在使用的方法,创建一个“加载更多”按钮,以便一次只加载15个(例如,可以是任意大小的)缩略图 为了收集应该显示的文件,我使用PHP的DirectoryIterator。我对每个页面都使用相同的自定义函数,但函数本身有点长,有点乱。基本上,我需要能够将文件放入适当的目录中,并自动检测它们,并显示带有完整图像链接的缩略图 我已经有了这个设置和工作。功能的一半还检查任何子目录,并重复这些子目录的过程,这样我就可

我在我的WordPress网站上有很多“图像库”样式的页面。我正试图找出是否有可能,使用我正在使用的方法,创建一个“加载更多”按钮,以便一次只加载15个(例如,可以是任意大小的)缩略图

为了收集应该显示的文件,我使用PHP的DirectoryIterator。我对每个页面都使用相同的自定义函数,但函数本身有点长,有点乱。基本上,我需要能够将文件放入适当的目录中,并自动检测它们,并显示带有完整图像链接的缩略图

我已经有了这个设置和工作。功能的一半还检查任何子目录,并重复这些子目录的过程,这样我就可以在一个页面上创建目录和上载文件,拥有任意数量的“图库”,每个图库都有任意数量的图像

它相当长,但作为参考,我觉得在问这个问题时需要粘贴完整的函数,以便人们知道我在用什么。我正在处理的内容类型是视频游戏截图

function display_screenshots() {
  global $post;
  $post_data = get_post($post->post_parent);
  $parent_slug = $post_data->post_name;
  $img_directory = '/wp-content/uploads/screenshots/' . $parent_slug . '/';
  $thumb_directory = '/wp-content/uploads/screenshots/' . $parent_slug . '/thumb/';
  $img_list_directory = getcwd() . '/wp-content/uploads/screenshots/' . $parent_slug . '/';
  if(is_dir($img_list_directory)) {
    $found_shots = array();
    echo '<div class="screenshot-thumbs-outer">';
    echo '<div class="screenshot-thumbs-inner">';
    foreach(new DirectoryIterator($img_list_directory) as $file) {
        if ($file->isDot()) continue;
        $fileName = $file->getFilename();
        $filetypes = array(
            "png",
            "PNG"
        );
        $filetype = pathinfo($file, PATHINFO_EXTENSION);
        if (in_array(strtolower($filetype), $filetypes )) {
            $found_shots[] = array( 
                "fileName" => $fileName
            );       
        }
    }
    asort($found_shots);
    foreach($found_shots as $file) {
        $filename = $file['fileName'];
        $thumbname = substr($filename, 0, -3).'jpg';
        echo '<a href="#" data-featherlight="'.$img_directory.$filename.'" rel="nofollow">';
        echo '<img src="'.$thumb_directory.$thumbname.'"/>';
        echo '</a>';
    }
    echo '</div>';
    echo '</div>';
  }
  if(is_dir($img_list_directory)) {
    foreach(new DirectoryIterator($img_list_directory) as $dir) {
        if($dir->isDir() && $dir != '.' && $dir != '..' && $dir != 'thumb') {
            $dir_h2 = substr($post->post_title, 0, -12) . ' - ' . str_replace(array('Hd ', ' Hd ', ' Of ', ' The ', ' A ', ' To '), array('HD ', ' HD ', ' of ', ' the ', ' a ', ' to '), ucwords(str_replace("-", " ", $dir))) . ' Screenshots';
            $img_directory = '/wp-content/uploads/screenshots/' . $parent_slug . '/' . $dir . '/';
            $thumb_directory = '/wp-content/uploads/screenshots/' . $parent_slug . '/' . $dir . '/thumb/';
            $img_list_directory = getcwd() . '/wp-content/uploads/screenshots/' . $parent_slug . '/' . $dir . '/';
            $found_shots = array();
            echo '<hr />';
            echo '<h2>' . $dir_h2 . '</h2>';
            echo '<div class="screenshot-thumbs-outer">';
            echo '<div class="screenshot-thumbs-inner">';
            foreach(new DirectoryIterator($img_list_directory) as $file) {
                if ($file->isDot()) continue;
                $fileName = $file->getFilename();
                $filetypes = array(
                    "png",
                    "PNG"
                );
                $filetype = pathinfo($file, PATHINFO_EXTENSION);
                if (in_array(strtolower($filetype), $filetypes )) {
                    $found_shots[] = array( 
                        "fileName" => $fileName
                    );       
                }
            }
            asort($found_shots);
            foreach($found_shots as $file) {
                $filename = $file['fileName'];
                $thumbname = substr($filename, 0, -3).'jpg';
                echo '<a href="#" data-featherlight="'.$img_directory.$filename.'" rel="nofollow">';
                echo '<img src="'.$thumb_directory.$thumbname.'"/>';
                echo '</a>';
            }
            echo '</div>';
            echo '</div>';
        }
    }
  }
}
功能显示\u屏幕截图(){
全球$员额;
$post\u data=get\u post($post->post\u父项);
$parent\u slug=$post\u data->post\u name;
$img_directory='/wp content/uploads/screenshots/'.$parent_slug./';
$thumb_directory='/wp content/uploads/screenshots/'。$parent_slug./thumb/';
$img_list_directory=getcwd()。/wp content/uploads/screenshots/。$parent_slug./;
if(is_dir($img_list_directory)){
$found_shots=array();
回声';
回声';
foreach(新目录迭代器($img\u list\u目录)作为$file){
如果($file->isDot())继续;
$fileName=$file->getFilename();
$filetypes=array(
“巴布亚新几内亚”,
“巴布亚新几内亚”
);
$filetype=pathinfo($file,pathinfo_扩展名);
if(在数组中(strtolower($filetype),$filetypes)){
$found_shots[]=数组(
“fileName”=>$fileName
);       
}
}
asort($found_shots);
foreach($found\u shots作为$file){
$filename=$file['filename'];
$thumbname=substr($filename,0,-3)。'jpg';
回声';
}
回声';
回声';
}
if(is_dir($img_list_directory)){
foreach(新目录迭代器($img\u list\u目录)为$dir){
如果($dir->isDir()&&&$dir!='.&&&$dir!='..'&&&$dir!='thumb'){
$dir_h2=substr($post->post_title,0,-12)。'-'.str_替换(数组('Hd','Hd','Of','The','A','The','A','To'),数组('Hd','Of','Of','The','A','To'),ucwords(str替换(“-”,','Hd Of','To'))。'Screenshots';
$img_directory='/wp content/uploads/screenshots/'.$parent_slug./'.$dir./';
$thumb_directory='/wp content/uploads/screenshots/'.$parent_slug./'.$dir./thumb/';
$img_list_directory=getcwd()。/wp content/uploads/screenshots/'。$parent_slug./'。$dir./';
$found_shots=array();
回声“
”; 回音“.$dir_h2.”; 回声'; 回声'; foreach(新目录迭代器($img\u list\u目录)作为$file){ 如果($file->isDot())继续; $fileName=$file->getFilename(); $filetypes=array( “巴布亚新几内亚”, “巴布亚新几内亚” ); $filetype=pathinfo($file,pathinfo_扩展名); if(在数组中(strtolower($filetype),$filetypes)){ $found_shots[]=数组( “fileName”=>$fileName ); } } asort($found_shots); foreach($found\u shots作为$file){ $filename=$file['filename']; $thumbname=substr($filename,0,-3)。'jpg'; 回声'; } 回声'; 回声'; } } } }
同样,这个功能正在按预期工作,我只是想找到一些方法来添加“加载更多”功能,这样,如果有150个图像,一次只加载15个。我还需要在函数的后半部分复制“加载更多”按钮的代码,这样页面上的任何其他库也会出现这种情况

我意识到这对其他人来说是一个很大的挑战,但我花了很多时间在这个网站上梳理答案,搜索谷歌,我还没有找到一个真正与我用来收集/显示这些图像的功能相关的解决方案

任何帮助都将不胜感激


谢谢

你需要学习递归,它会简化你的代码是的,我想可能有一种更干净的方法来实现这个函数。这不是我想要帮助的,但你绝对是对的。我将研究递归,谢谢。:)你需要学习递归,它会简化你的代码是的,我想可能有一种更干净的方法来实现这个函数。这不是我想要帮助的,但你绝对是对的。我将研究递归,谢谢。:)