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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

372 views 20 10
Join discussion

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