简体   繁体   English

WordPress php glob(); 不工作?

[英]WordPress php glob(); not working?

I have created a function in WordPress where I wish to obtain all the images within a given directory, for which I am using the PHP glob function, for some reason I cannot get this to work, is the glob() function disabled for use within WordPress? 我在WordPress中创建了一个函数,我想获取给定目录中的所有图像,我正在使用PHP glob函数,由于某种原因我无法使用它,是否禁用了glob()函数WordPress的?

The Code that Doesn't Work... 不起作用的代码......

function getAccreditaionLogos(){

    define('ACCREDPATH', get_stylesheet_directory_uri() . '/img/accreditations/');

    $images = glob(ACCREDPATH . '*.png');
    foreach($images as $key => $img):
        $get_icons = '<li><img src="'.$img.'" /></li>';
        echo $get_icons;
    endforeach;
}

The function get_stylesheet_directory_uri() gives you a web url ( http://… ) . 函数get_stylesheet_directory_uri()为您提供了一个网址(http:// ...)。 You have to use an absolute system path. 您必须使用绝对系统路径。 You can get it by using the get_theme_root() function instead. 您可以使用get_theme_root()函数来获取它。

Your function should look like this: 你的功能应该是这样的:

function getAccreditaionLogos(){

    define('ACCREDPATH', get_theme_root() . '/img/accreditations/');

    $images = glob(ACCREDPATH . '*.png');
    foreach($images as $key => $img):
        $get_icons = '<li><img src="'.$img.'" /></li>';
        echo $get_icons;
    endforeach;
}

More details of this function in the Wordpress Codex . Wordpress Codex中此功能的更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM