0

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

I also tried get_template_directory_uri() but still getting empty array

4

You should use paths with glob, not URLs.

But src attributes needs URLs.

So something like this should work:

$base_dir = trailingslashit(get_template_directory());
$dir      = 'img/services/';
$images   = glob($base_dir.$dir.'*.png');

foreach($images as $image) {
    $url = get_theme_file_uri($dir.basename($image));
    printf('<div class="dynamic item"><img src="%s" alt=""></div>', esc_url($url));
}

Where I make use of:

  • get_template_directory To obtain the root path of (parent) theme
  • basename to obtain just the file name of the current image file path
  • get_theme_file_uri to obtain the full URL of the image, in a child-theme friendly way: if the image is found in child theme it will be used from there, otherwise will be used from parent theme.
0

I think you just have to try get_stylesheet_directory().

$images = glob(get_stylesheet_directory()."/img/services/*.png");

You're using glob on an URL, not a path actually.

  • Can you try to dump this : var_dump(get_stylesheet_directory()."/img/services/"); – Elex Dec 19 '17 at 17:26
  • Are you trying to get the images from another website with your glob ? – Elex Dec 19 '17 at 17:30
  • No they are in theme image folder – Mona Coder Dec 19 '17 at 17:33
  • Ok, so you're sure that this path contain your images ? And that /home/vcbb/public_html/wp-content/themes/VanBeautyBoot/img/‌​services/ exist ? – Elex Dec 19 '17 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.