Display random photo attachment from another page and its child pages

Trying to grab a random attachment from a photo gallery page - located on a child page, uses the gallery macro - and it's child-pages (the sub-albums).

First, I'd love to know if there's a better way for managing a photo gallery in Confluence.

Second, and my main question, how do I do this? I found a code example using the report-image macro but can't get it to work:

- Home page

- - W page (this is the page I want to display a random image on)

- - - X page (page containing various content, DON'T WANT to get anything from this)

- - - A page (this is a gallery page, may get a random photo/attachment from here)

- - - - B page (sub/child-page of A, could also get random photo from here)

- - - - C page (as B)

- - - - D page (as B)

- - - Y page (another page, don't want to get anything here!)

- - - Z page (as Y)

In short, get random image (attachment) from A, B, C, or D and display on W - but don't get anything from X Y Z or anywhere else in the space.

TIA!

1 answer

1 accepted

1 vote
Answer accepted

Hi Jason,

You should be able to use something like this:

{report-variable:Images}
{expanding-reporter:page:attachments|as=Attachment}
  {combining-reporter}
    {local-reporter:@self|source=A page}
    {local-reporter:page:descendents|source=A page}
  {combining-reporter}
  {text-filter:attachment:content type|include=image.*}
{expanding-reporter}
{report-variable}

{report-image:variable:Images > query:as collection > collection:random > expanded:Attachment > attachment:url}

Essentially, it creates a combination list of 'A page' (@self) with the descendants of 'A page' (yes, 'descendents' is spelled that way in the supplier. It's a typo I should fix in the code sometime...). Then, it expands on the attachments of said pages that have a content type starting with 'image'. So, we end up with a big list of all attachments, stored in 'expanded' objects. Then, we select a 'random' item from the complete list and grab it's URL for the {report-image}.

Hope that helps!

David

Thank you David! It sure did, and thank you for making it so clear - plug and play. Beautiful! Have a great day!

This sounds like exactly what I'm trying to accomplish, but unfortunately I'm too much of a non-coder to understand what to do with this. Do I use this code to create a User Macro?

This doesn't work for me - I'm trying to place the macro in a panel if that's relevant. I'm just pasting in the macro you wrote, changing the page names. Do you know what I can change? 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

409 views 2 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you