【问题标题】:WordPress paths with GLOB PHP带有 GLOB PHP 的 WordPress 路径
【发布时间】:2011-11-10 22:00:52
【问题描述】:

您好,我正在使用下面的代码尝试读取一个目录并显示其中的所有 JPEG,但由于 WordPress 路径,我无法解决问题:

<?php 
    $path = get_bloginfo('template_directory');
    $files = glob("homepageBottomPictures/*.*");
    var_dump($files);
    for ($i=1; $i<count($files); $i++) {
        $num = $files[$i];
        echo '<img src="'.$num.'" alt="random image" class="homepageBtmImg">'."&nbsp;&nbsp;";
    } 
    echo '<div class="clearfix"></div>';
?>

所以在我的主题目录中,我有一个名为 homepageBottomPictures 的文件夹,其中包含 JPEG。我能做些什么? (目前,$files 没有返回任何内容)

【问题讨论】:

  • 这个 php 脚本与homepageBottomPictures/ 的关系在哪里?同一个文件夹?

标签: php wordpress glob


【解决方案1】:

尝试使用: get_theme_root() . get_template() . '/homepageBottomPictures/*.*' 获取您的目录

【讨论】:

    【解决方案2】:

    您可能会发现这很有用,这是我编写的一些代码,用于获取文件名中具有特定模式的几种类型的图像文件。您可以对其稍作修改,以仅提取任何模式的 jpg。

    function returnimages($dirname, $photo_id) {
    
            $pattern="(^".$photo_id."[A-Za-z0-9_]*\.jpg$)|(^".$photo_id."[A-Za-z0-9_]*\.png$)|(^".$photo_id."[A-Za-z0-9_]*\.jpeg$)|(^".$photo_id."[A-Za-z0-9_]*\.gif$)"; //valid image extensions
            $files = array();
            $curimage=0;
    
            if($handle = opendir($dirname)) {
                while(false !== ($file = readdir($handle))){
                    if(eregi($pattern, $file)){         //if this file is a matching image
                        $files[$curimage] = $dirname.$file;   //Save it in the array
                        $curimage++;
                    }//end if
                }//end while
    
                closedir($handle);
            }//end if   
    return($files);
    
    }//end returnimages 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2011-05-02
      相关资源
      最近更新 更多