Random images image.php
-
How can I show random images on my image.php page? Some thumbnails of particular posts?
Don’t want to use a plugin.
Using twentyTwelve theme
Thank you
-
so I found this, but is there a way to specify how many images show, and stop duplicate images?
<?php $query = new WP_Query( array( 'post_status' => 'any', 'post_type' => 'attachment' ) ); $key = array_rand($query->posts, 1); echo wp_get_attachment_image($query->posts[$key]->ID, 'thumbnail'); ?>[Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
Thanks
Do not edit the Twenty Twelve theme. It is the default WordPress theme and having access to an unedited version of the theme is vital when dealing with a range of site issues. First create a child theme for your changes.
way ahead of you on that Esmi. I always use child themes. Got any ideas on how to do it?
Have you had a look at http://wordpress.stackexchange.com/questions/11662/get-all-images-in-media-gallery
not sure about that link. Can you tell me what the link is to take me to an image url?
Thanks
wp_get_attachment_url() is the WordPress function that will actually output an image url – like
http://example.net/wp-content/uploads/filename.jpg. o:$query_images_args = array( 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_images = new WP_Query( $query_images_args ); $images = array(); foreach ( $query_images->posts as $image) { $images[]= wp_get_attachment_url( $image->ID ); }will grab all of the images in the media library & dump their urls into an array called $images. You can use that array of image urls in any way that you want. If you want a couple of random images, you could use PHP’s rand function to generate a couple of random numbers between 0 and
ount( $image )(which is the number of items in the image array.making sure that one of these random images isn’t a replica of the main displayed image could be tricky, though.
I worded that incorrect – I need this next_attachment_url to take me to the image page. Not a refresh with a new image. I need it to take me to that images url, not the jpg url. Any ideas?
Thanks
by the way this doesn’t work:
<?php $query_images_args = array( 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_images = new WP_Query( $query_images_args ); $images = array(); foreach ( $query_images->posts as $image) { $images[]= wp_get_attachment_url( $image->ID ); } ?>Can someone please point out code that will show a random thumbnail in twentytwelve theme with a link to its post page?
Thank you
The code esmi provided works, but it is not a turnkey solution. You need to do some of the work. The code just stuffs a bunch of attachment IDs into an array. The code you develop needs to randomly grab one ID. With the ID, you can get the image source with
wp_get_attachment_image_src()and the image’s page permalink withget_permalink(). With all that you can output all the HTML required to show a random thumbnail with a link to it’s page.If you wrap this all in a function, you can call the function from your child theme’s template where ever you want the thumbnail to appear.
The topic ‘Random images image.php’ is closed to new replies.