Membership is FREE, giving all registered users unlimited access to every DNForum feature, resource, and tool! Optional membership upgrades unlock exclusive benefits like profile signatures with links, banner placements, appearances in the weekly newsletter, and much more - customized to your membership level!

How to display a random image in this code?

Status
Not open for further replies.

kokopelli

Level 8
Legacy Platinum Member
Joined
Jul 21, 2004
Messages
1,001
Reaction score
1
Hope one of you PHP gurus can help me ... :)

In a WordPress theme, I want to change this code:
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' )); } ?>

to something like
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_0<?php echo rand(1,3)?>.png[/color]', 'width' => '100', 'image_class' => 'wp-post-image' )); } ?>
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?
 

NamePrime

nameprime.com
Legacy Platinum Member
Joined
Feb 15, 2010
Messages
839
Reaction score
18
I think putting it inside the <?php ?> block is not right. Try to put it in a variable first then concatenate. Try something like this one:

<?php $randomImg = rand(1,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'. $randomImg .'.png', 'width' => '100', 'image_class' => 'wp-post-image' )); } ?>
 

kokopelli

Level 8
Legacy Platinum Member
Joined
Jul 21, 2004
Messages
1,001
Reaction score
1
Cool, that works (I had to put variable in the WordPress functions file).

Thanks! :)
 
Status
Not open for further replies.

Who has viewed this thread (Total: 1) View details

Who has watched this thread (Total: 1) View details

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Premium Members

Upcoming events

Latest Listings

Our Mods' Businesses

*the exceptional businesses of our esteemed moderators

Top Bottom