dongqingcheng2903 2017-12-19 16:37
浏览 48
已采纳

有关在WordPress中使用PHP glob()的问题

Using pure PHP code inside WordPress I am having trouble on getting the glob() work to generate image source.

<div class="carousel-inner" role="listbox" style="height=600px; width=1000px;">
    <?php
     $directory = "http://geocaa.com/wp-content/themes/Booting/img/services/";
     $images = glob($directory . "*.png");
      foreach($images as $image)
       {
        echo '<div class="dynamic item">';
        echo ' <img src="'.$image.'" alt="...">';
        echo ' </div>';
       }
    ?>
</div>

As you can see I tried to hardcoded the $directory as "http://geocaa.com/wp-content/themes/Booting/img/services/"; and also I already investigate on these two Post [Post 1 & Post 2 ] regarding the same issues but the solutions there still not working for me!

The get_theme_root() retuns nothing but the get_template_directory() is returning something which is more like

$images = glob(get_template_directory().$directory . "*.png");

/home/vcbb/public_html/wp-content/themes/geocaa/img/services/img.png

and useless for image src

  • 写回答

1条回答 默认 最新

  • duanliang789262 2017-12-19 17:21
    关注

    Try this:

    $directory = "/img/services/";
    $images = glob(get_template_directory().$directory . "*.png");
    foreach($images as $image)
    {
        echo '<div class="dynamic item">';
        echo ' <img src="'. str_replace(get_home_path(), get_home_url(), $image) .'" alt="...">';
        echo ' </div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵