674 posts
  • Won a competition
  • Had an item featured in an Envato Bundle
  • Has sold $10,000+ on Envato Market
  • Located in Australia
+7 more
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!

674 posts
  • Won a competition
  • Had an item featured in an Envato Bundle
  • Has sold $10,000+ on Envato Market
  • Located in Australia
+7 more
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
  • Has referred 1+ members
  • Has sold $5,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+2 more
dekciw says
109 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+4 more
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!

109 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+4 more
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!

674 posts
  • Won a competition
  • Had an item featured in an Envato Bundle
  • Has sold $10,000+ on Envato Market
  • Located in Australia
+7 more
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!

109 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+4 more
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 :)

674 posts
  • Won a competition
  • Had an item featured in an Envato Bundle
  • Has sold $10,000+ on Envato Market
  • Located in Australia
+7 more
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..

674 posts
  • Won a competition
  • Had an item featured in an Envato Bundle
  • Has sold $10,000+ on Envato Market
  • Located in Australia
+7 more
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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by