Forums

Using glob in themes functions.php (9 posts)

  1. Anton Niklasson
    Member
    Posted 2 years ago #

    Hey guys, I really need some help figuring this out.

    I would like to randomly select an image from a folder within my theme. I have a custom function in the functions.php which makes the call and echoes the correct html code.

    This is what the function looks like:

    $path = "wp-content/themes/white/imgs/header/";
    
    $images = glob($path."*.{jpg,png,gif}", GLOB_BRACE);
    shuffle($images);
    
    echo "<img src='".$images[0]."'>";

    The problem is that when using permalinks like http://www.example.com/products/something the relative path given in the function won't work. Is there some built in functionality for solving this problem?

    I was thinking about sending the current URL when calling the function. And from there figure out how to write the relative path including the number of "../" it takes to get it right.

    Anyone?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

  3. Anton Niklasson
    Member
    Posted 2 years ago #

    I am well aware of those functions, and I did try to use them.

    Is this a misunderstanding, or is it not allowed to put absolute paths like "http://www.google.com/something/whatever/*" in the glob pattern?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    glob is for searching path name - not urls.

    http://uk2.php.net/manual/en/function.glob.php

  5. Anton Niklasson
    Member
    Posted 2 years ago #

    Yeah ok. So how would you go about and do it?

    I store the pictures in "imgs/header" inside the theme folder.
    Is there another approach then using glob for this?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    $path = get_stylesheet_directory() . "/imgs/header/";

  7. Anton Niklasson
    Member
    Posted 2 years ago #

    Yeah I did try that.

    The problem is that get_stylesheet_directory() returns this value: "/customers/antonniklasson.se/antonniklasson.se/httpd.www/code/wordpress/wp-content/themes/white"

    And that does not work well with the permalinks and so on...

  8. kreegah
    Member
    Posted 1 year ago #

    AntonNiklasson: Did you ever find a solution to this?

    I am trying to add a hardcoded fetch image in directory thing in one of my templates, and

    <?php $files = glob(ABSPATH. "wp-content/uploads/coverflow/*.*");
    for ($i=1; $i<count($files); $i++)
    {
    	$num = $files[$i];
    	echo '<li><div class="item"><img src="'.$num.'" height="100px" width="100px" /></div></li>';} ?>

    I get placeholders for the images, but the url to each image reads as: <http://www.sitename.com/home/username/public_html/wp-content/uploads/coverflow/image.jpg>

    How can I edit this to be my constant url?
    i.e :<http://www.sitename.com/wp-content/..>

  9. kreegah
    Member
    Posted 1 year ago #

    Ok! I got it to work, for my need atleast:

    <?php $img = glob("wp-content/uploads/coverflow/*.jpg");
    for ($i=1; $i<count($img); $i++)
    {	$num = $img[$i];
    	echo '<li><div class="item"><img src="http://www.siteurl.com/'.$num.'" height="75px" width="75px" /></div></li>';} ?>

    Now what I would like is to implement a preloader of all the images within this php markup...

Topic Closed

This topic has been closed to new replies.

About this Topic