How to display a random image in this code?

Discussion in 'PHP' started by kokopelli, Oct 13, 2010.

  1. #1
    Hope one of you PHP gurus can help me ... :)

    In a WordPress theme, I want to change this code:
    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image(array( 'default_size' => 'thumbnail', 'image_scan' => true, 'default_image' => get_bloginfo( 'template_url' ) . '/images/[color=#FF0000]default_thumbnail.png[/color]', 'width' => '100', 'image_class' => 'wp-post-image' )); } ?>
    Code (markup):
    to something like
    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image(array( 'default_size' => 'thumbnail', 'image_scan' => true, 'default_image' => get_bloginfo( 'template_url' ) . '/images/[color=#FF0000]default_thumbnail_0<?php echo rand(1,3)?>.png[/color]', 'width' => '100', 'image_class' => 'wp-post-image' )); } ?>
    Code (markup):
    so a random image between 1 and 3 is displayed as thumbnail.

    But my code doesn't work ... no image is displayed :confused:

    Any idea how I should change it?
     
    kokopelli, Oct 13, 2010 IP
  2. sunlcik

    sunlcik Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are these files exist?

    /images/default_thumbnail_01.png , /images/default_thumbnail_02.png , /images/default_thumbnail_03.png
     
    sunlcik, Oct 13, 2010 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    <?php if ( function_exists( 'get_the_image' ) ) { get_the_image(array( 'default_size' => 'thumbnail', 'image_scan' => true, 'default_image' => get_bloginfo( 'template_url' ) . '/images/default_thumbnail_0' . rand(1,3) . '.png', 'width' => '100', 'image_class' => 'wp-post-image' )); } ?>
    PHP:
    Still doesn't work ?
     
    ActiveFrost, Oct 13, 2010 IP
    kokopelli likes this.
  4. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #4
    
    
    <?php 
    //set the number you want to randomize between first.
    $randomize = rand(1, 3);
    
    //check and display the random image now
    if ( function_exists( 'get_the_image' ) ) { get_the_image(array( 'default_size' => 'thumbnail', 'image_scan' => true, 'default_image' => get_bloginfo( 'template_url' ) . '/images/default_thumbnail_0' . $randomize . '.png', 'width' => '100', 'image_class' => 'wp-post-image' )); } 
    
    ?>
    PHP:
     
    lowridertj, Oct 13, 2010 IP
  5. kokopelli

    kokopelli Peon

    Messages:
    2,436
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @sunlcik
    Yes, they do.

    @ActiveFrost
    That did the trick, thanks! :) (Added to your rep)

    @lowridertj
    That also works (got a similar solution on another forum), but I like ActiveFrost's solution for its simplicity. Thanks anyway. :)

    Thanks guys, problem solved.
     
    kokopelli, Oct 13, 2010 IP
  6. sinha.sidhi

    sinha.sidhi Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    one another method all image store in the database and use select random order query and show in the front and for animation you will use java script.
     
    sinha.sidhi, Aug 29, 2011 IP