WordPress php glob(); not working?

£可爱£侵袭症+ 提交于 2019-12-30 07:33:33

问题


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?

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;
}

回答1:


The function get_stylesheet_directory_uri() gives you a web url ( http://… ) . You have to use an absolute system path. You can get it by using the get_theme_root() function instead.

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.



来源:https://stackoverflow.com/questions/19295175/wordpress-php-glob-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!