• Resolved JohnWinch

    (@johnwinch)


    Hello,

    I would like to display a random logo from a specific folder. I’ve found the following script, it’s perfectly working on a test.php located in my folder theme but nothing is displaying when I put the code in header.

    Here’s the code :

    <?php
    // Ouvre un dossier bien connu, et liste tous les fichiers
    $directory = 'images/logo';
    // Définition d'$image comme tableau
    $image = array();
     //on vérifie s’il s’agit bien d’un répertoire
     if (is_dir($directory))
     {
     //on ouvre le repertoire
     if ($dh = opendir($directory))
     {
     //Lit une entrée du dossier et readdir retourne le nom du fichier
     while (($file = readdir($dh)) !== false)
     {
     // Vérifie de ne pas prendre en compte les dossier ...
     if ($file != '...' && $file != '..' && $file != '.')
     {
     // On ajoute le nom du fichier dans le tableau
     $image[] = $file;
     }
     }
     //On ferme le repertoire
     closedir($dh);
     // On récupère le nombre d'image total
     $total = count($image)-1;
     // On prend une valeur au hasard entre 1 et le nombre total d'images
     $aleatoire = rand(0, $total);
     // On récupère le nom de l'image avec le chiffre hasard
     $image_afficher = "$image[$aleatoire]";
     // Affiche l'image du hasard :p
     print "<img src='$directory/$image_afficher' border='0'>";
     }
     }
    ?>

    Is there a part of this code incompatible with WordPress ?

    Thanks you for your help.

    Best regards,

Viewing 13 replies - 1 through 13 (of 13 total)
  • I think you need to change directory path to
    $directory = TEMPLATEPATH . ‘/images/logo’;

    Thread Starter JohnWinch

    (@johnwinch)

    Thanks you for your answer but it didn’t work.
    Is there an another way to display randomly a logo from a folder ?

    $directory = TEMPLATEPATH . '/images/logo';
    $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';

    […]
    print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';

    Thread Starter JohnWinch

    (@johnwinch)

    Thanks, I’ve tried it but it didn’t work either.
    Here’s my code, i don’t think I’ve made any mistakes.

    <?php
    // Ouvre un dossier bien connu, et liste tous les fichiers
    $directory = TEMPLATEPATH . '/images/logo';
    $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
    // Définition d'$image comme tableau
    $image = array();
     //on vérifie s’il s’agit bien d’un répertoire
     if (is_dir($directory))
     {
     //on ouvre le repertoire
     if ($dh = opendir($directory))
     {
     //Lit une entrée du dossier et readdir retourne le nom du fichier
     while (($file = readdir($dh)) !== false)
     {
     // Vérifie de ne pas prendre en compte les dossier ...
     if ($file != '...' && $file != '..' && $file != '.')
     {
     // On ajoute le nom du fichier dans le tableau
     $image[] = $file;
     }
     }
     //On ferme le repertoire
     closedir($dh);
     // On récupère le nombre d'image total
     $total = count($image)-1;
     // On prend une valeur au hasard entre 1 et le nombre total d'images
     $aleatoire = rand(0, $total);
     // On récupère le nom de l'image avec le chiffre hasard
     $image_afficher = "$image[$aleatoire]";
     // Affiche l'image du hasard :p
    print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
     }
     }
    ?>

    Here’s the path to my theme file :
    https://127.0.0.1/blog/wp-content/themes/fashionpro/

    My logo’s folder :
    https://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/

    Try this:

    <?php
    $directory = TEMPLATEPATH . '/images/logo';
    $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
    $image = array();
    if ( is_dir($directory) && $dh = opendir($directory) ) {
    	while (($file = readdir($dh)) !== false) {
    		if ($file != '...' && $file != '..' && $file != '.') {
    			$image[] = $file;
    		}
    	}
    	closedir($dh);
    	$total = count($image)-1;
    	$aleatoire = rand(0, $total);
    	$image_afficher = $image[$aleatoire];
    	print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
    }
    ?>
    Thread Starter JohnWinch

    (@johnwinch)

    It didn’t work, but when I look at the source code from Firefox, here’s how the code is displayed :

    <img src=”https://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/l.jpg%20border=&#8221; 0=””>

    This must have something to do with the %20 after the .jpg

    Check what’s in the $image array.

    Thread Starter JohnWinch

    (@johnwinch)

    What should I be looking for ? I’m new to this but if you explain to me it might help me understand.

    <?php
    $directory = TEMPLATEPATH . '/images/logo';
    $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
    $image = array();
    if ( is_dir($directory) && $dh = opendir($directory) ) {
    	while (($file = readdir($dh)) !== false) {
    		if ($file != '...' && $file != '..' && $file != '.') {
    			$image[] = $file;
    		}
    	}
    	closedir($dh);
            echo '<pre>';
            print_r($image);
            echo '</pre>';
    	$total = count($image)-1;
    	$aleatoire = rand(0, $total);
    	$image_afficher = $image[$aleatoire];
    	print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
    }
    ?>
    Thread Starter JohnWinch

    (@johnwinch)

    It’s not working but when I put my mouse over the place where the logo should be displayed I’ve got the following :

    Array
    (
    [0] => l.jpg
    [1] => logo1.jpg
    [2] => logo2.jpg
    [3] => logo3.jpg
    [4] => zeatrt.jpg
    [5] => zergter.jpg
    [6] => zerzt.jpg
    )

    Remove:

    echo '<pre>';
    print_r($image);
    echo '</pre>';

    and change:

    print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';

    to:

    echo '<img src="' . $directory_uri . $image_afficher . '" />';

    Thread Starter JohnWinch

    (@johnwinch)

    Thanks you ! It’s working !

    Excellent! ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Random logo script not working ?’ is closed to new replies.