you want to use 'orderby' => 'rand()'
for that.
if ( $images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1,
'orderby' => 'rand()',
'post_mime_type' => 'image',))){
There’s a problem with this method that actually caused me to remove this feature from the site I was working on. The client didn’t mind since it was already graphics-intensive. But I didn’t get this thing to work properly so I suggest you look for a plugin.
Mores,
Thanks for the reply!
How do you set which category would be used in that code? Could you post the full code that you had working?
Thanks again
Here’s my entire random image function that I used in the functions.php.
function randomimage_mores($size=medium,$num=1,$cat=0) {
$posts=query_posts('cat='.$cat);
foreach( $posts as $post ) {
if ( $images = get_children(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => $num,
'orderby' => 'rand()',
'post_mime_type' => 'image',)))
{
$zeiger = 1;
foreach( $images as $image ) {
if ($zeiger != $num) {
$zwischen=" ";
} else {
$zwischen = "";
$zeiger = 1;
}
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image( $image->ID, $size );
echo '<a href="'.$attachmenturl.'" rel="lightbox">'.$attachmentimage.'</a>'.$zwischen;
$zeiger++;
}
} else {
echo "No Image";
}
}
}
But, like I said, it didn’t behave like I wanted it to, so I dropped it. Maybe it’ll work for you, though.
Thanks Mores!
Now it pulls images from the set cat. I’ve just got to figure out how to limit it to 1 attachment to show rather than the random 10 that it’s doing.
Use the $zeiger variable. I used it insert spaces between the images but the last image in a row does not get the added spaces.
Like, if there are 10 images, you only add a bump after the first nine …
You can use it to quit after the desired number of images.
Thanks again for the reply!
I have got to be making this harder than it should be. No matter what I try for the $zeiger variable, I either get 10 images showing, or 1k+. I don’t see what is defining for it to originally pull 10 images rather than the number set for $num.
Thank you again
Hmmm … maybe the $posts=query_posts('cat='.$cat);
thing pulls the maximum number of posts, which are defined in your WP settings.
Try adding using $posts=query_posts('showposts=1&cat='.$cat);
Perfect! Thank you soo much Mores!
Best Regards!
Shane
Well it seems to be showing just fine. My only problem now is it pulls attachments even if they are not in the set category. If you happen to know another way to set a category, I’d very much appreciate it. If not, I’ll keep playing w/ it and searching, but for now it’s much much closer and will work 🙂
<?php function randomimage($size=medium,$num=1,$cat=0) {
$posts=query_posts('showposts=1&cat='.$cat);
foreach( $posts as $post ) {
if ( $images = get_children(array(
'post_type' => 'attachment',
'numberposts' => $num,
'orderby' => 'rand()',
'post_mime_type' => 'image',)))
{
$zeiger = 1;
foreach( $images as $image ) {
if ($zeiger != $num) {
$zwischen=" ";
} else {
$zwischen = "";
$zeiger = 1;
}
$attachmenturl=get_permalink($image->ID);
$attachmentimage=wp_get_attachment_image( $image->ID, $size );
echo '<a href="'.$attachmenturl.'">'.$attachmentimage.'</a>'.$zwischen;
$zeiger++;
}
} else {
echo "No Image";
}
}
}
?>
One quick bump, hoping mores or someone else could help me fix the category issue. It’s not limiting the category to what I choose.
Thanks
Do you call the function properly?
<?php randomimage('thumbnail','1','6'); ?>
(6 is just an example!)
Yessir, exactly like your example.
Thank you again!
Anyone have any other ideas I could try?
Thanks!