Mona Coder
Mona Coder

Reputation: 6316

Having Issues on Using PHP glob() in WordPress

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

Upvotes: 0

Views: 170

Answers (1)

Ying Hong
Ying Hong

Reputation: 36

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

Upvotes: 1

Related Questions