WordPress.org

Ready to get started?Download WordPress

Forums

Random images image.php (11 posts)

  1. Famous
    Member
    Posted 1 year ago #

    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

  2. Famous
    Member
    Posted 1 year ago #

    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

  3. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    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.

  4. Famous
    Member
    Posted 1 year ago #

    way ahead of you on that Esmi. I always use child themes. Got any ideas on how to do it?

  5. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  6. Famous
    Member
    Posted 1 year ago #

    not sure about that link. Can you tell me what the link is to take me to an image url?

    Thanks

  7. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    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.

  8. Famous
    Member
    Posted 1 year ago #

    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

  9. Famous
    Member
    Posted 1 year ago #

    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 );
    }
    ?>
  10. Famous
    Member
    Posted 1 year ago #

    Can someone please point out code that will show a random thumbnail in twentytwelve theme with a link to its post page?

    Thank you

  11. bcworkz
    Member
    Posted 12 months ago #

    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 with get_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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags