498 posts
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Australia
  • Has been a member for 3-4 years
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Envato Studio (Microlancer) Beta Tester
simonswiss says

Hello!

I’ve been scratching my head for a while and googled, stack overflowed etc.. over and over but couldn’t get to achieve a thing that, hopefully, is feasible with WordPress.

I have a page (page content type) that i use as a gallery with lots of attached photos. On the bottom of each page of the website, i’d like to display 4 random photos from this gallery page (a sort of “from the gallery” widget). Every time the user loads (or refreshes) a page, 4 new random photos should show up.

I guess a good way to sum up what i’m trying to do is to have a random sneak peak at 4 photos from the gallery page, where all the photos are attached.

The gallery has the page id of 3789, but i can’t figure out how to display random image attachments from the page_id 3789..

Any help would be greatly appreciated! Thanks in advance for the input!

498 posts
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Australia
  • Has been a member for 3-4 years
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Envato Studio (Microlancer) Beta Tester
simonswiss says

I guess i could also use a plugin for a gallery – do you have any recommendation of a plugin (free or premium) that allows to display random photos of a given gallery via template or shortcode?

332 posts
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Has been a member for 4-5 years
  • Europe
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
dekciw says
108 posts
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • United Kingdom
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
designedbydash says
$args = array(
    'numberposts' => null,
    'post_type' => 'attachment',
    'post_parent' => 3789,
    'orderby' => 'rand'
);

$attachments = get_posts($args);

foreach($attachments as $attachment) {
    //Do whatever with $attachment
}

Not tested, but this should do the trick!

108 posts
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • United Kingdom
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
designedbydash says

The edit time needs to be corrected, annoying that I can’t edit the above and have to double post but I just realised you wanted it limited to 4 so you need to change the numberposts parameter to 4!

498 posts
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Australia
  • Has been a member for 3-4 years
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Envato Studio (Microlancer) Beta Tester
simonswiss says

Thanks a lot!

I’ve tried this code (with the correction ‘numberposts => 4) but nothing displays.

If i want to display each of the 4 image as a thumbnail (medium size), what shall i put in the foreach statement?

Sorry for my noob question and thanks for your help!

108 posts
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • United Kingdom
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Referred between 1 and 9 users
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
designedbydash says

Thanks a lot!

I’ve tried this code (with the correction ‘numberposts => 4) but nothing displays.

If i want to display each of the 4 image as a thumbnail (medium size), what shall i put in the foreach statement?

Sorry for my noob question and thanks for your help!
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image

That should do it :)

498 posts
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Australia
  • Has been a member for 3-4 years
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Envato Studio (Microlancer) Beta Tester
simonswiss says

I’m exactly on that page and i was trying the following code, but nothing displays :(

<?php $args = array(
'post_type' => 'attachment',
'numberposts' => 4,
'post_parent' => 3789,
'orderby' => 'rand'
);

$attachments = get_posts( $args );

foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'medium' );
} ?>

I’m wondering if i need to put something else than $attachment->ID for the $attachment_id parameter..

498 posts
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Australia
  • Has been a member for 3-4 years
  • Bought between 100 and 499 items
  • Referred between 10 and 49 users
  • Envato Studio (Microlancer) Beta Tester
simonswiss says

Whoops it’s working now! Yehaa, thanks a lot for your help! I am not sure why it didn’t work in the first place, but thanks i got it working now!

Simon

by
by
by
by
by
by