Jump to content

Get images from multiple pages by tag, then display one image randomly

Recommended Posts

Hello All! This is my first post here and am a newbie with PW.  So far I've been able to accomplish everything I've wanted to do dynamically in my first PW site, but ran into a big snag yesterday.

Ok here's the setup:  I have on my site 4 pages that deal with separate plumbing services. We offer a discount coupon for each service. For each service, I am displaying it's coupon image in the sidebar. Each coupon image resides in the images field of the individual rootParent parent services page, and each image has a tag of "coupon" assigned to it. So for example, I have a rootParent level "Drain Cleaning" page and it is in this page's images field that the Drain Cleaning coupon image resides with the tag "coupon."  The next coupon resides in the images field for my rootParent "Sewer Cleaning" page, and so on.

For each service page, I have some Hanna Code I wrote to include the coupon image in the sidebar of the rootParent and all of it's children:

$coupon = $page->rootParent->images->getTag('coupon');

This works great!  Now, here's my problem... For pages other than the service pages, like form confirmation pages, the Contact Us page, etc., I still want to display a coupon image in the sidebar, but I want to grab a random coupon image and display it.  So far, I have not found a way to successfully scan all page's images field, grab the images with the tag of coupon, then display a random image.  Here is the code I've come up with so far that is not working:

First, build the array of pages containing an image with the tag of coupon (this works):

$couponpages = $pages->find("images.tags=coupon");

Next, get the images tagged coupon from the array of pages containing them (this is where the code breaks and throws an error):

$coupons = $couponpages->images->getTag("coupon");

The error reads: "Fatal error: Call to a member function getTag() on a non-object." 

Beyond that, I would then want to fetch a random image and display it:

$coupon = $coupons->getRandom();
echo "<img alt='{$coupon->description}' src='{$coupon->url}' />";

If anyone can help me solve this it would be most appreciated!



Share this post

Link to post
Share on other sites

Hold the phone... I just solved it!  Just had to reverse step two and step three!  So Get a random page from the array first, then display the image with the tag of coupon:

$couponpages = $pages->find("images.tags=coupon");
$coupons = $couponpages->getRandom();
$coupon = $coupons->images->getTag("coupon");
echo "<img alt='{$coupon->description}' src='{$coupon->url}' />";


  • Like 1

Share this post

Link to post
Share on other sites

Hi Dave,
Welcome to PW and the forums... :-)
Glad you sorted it out but would like to suggest you do it differently...assuming I understood the question correctly...
If all you want is to grab one random image from a coupon page, there is probably no need to find several pages first. Why not grab ONE random page instead (one containing your coupon images) and display its coupon image? Assuming that a coupon page contains other images as well but you are only interested in the coupon image (i.e. tagged 'coupon'), something like this maybe...

//we only get one random coupon page (we use 'get' rather than 'find')
$couponimage = $pages->get("template=service-pages, sort=random")->images->getTag('coupon');


This code assumes there will always be a coupon image in those service pages.

Edited by kongondo
  • Like 5

Share this post

Link to post
Share on other sites
//we only get one random coupon page (we use 'get' rather than 'find')
$couponimage = $pages->get("template=service-pages, images.tags=coupon, sort=random")->images->getTag('coupon');

@Kongondo: Your code together with the pages selector from Dave should be a solid solution. - But haven't tested.

@Dave: Welcome.  :)

  • Like 3

Share this post

Link to post
Share on other sites

Hi Kongondo,

Thanks so much for your reply! I tried your code and it didn't work for me, most likely because I have a common template for all internal pages named "content-page" and not all pages in the site have a coupon image in them.  However, your code is a great example of using selectors to condense the steps I took  :)  I'm sure it would work if I had a separate template just for the service pages.  Thanks for your input - it helps me a lot!

  • Like 1

Share this post

Link to post
Share on other sites

Hello Horst,

Adding the "images.tags=coupon" selector to Kongondo's code DID THE TRICK!  You guys ROCK!  Thank you :)

  • Like 5

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By horst
      Hi, on a site I want to disable access to original images and only allow to access thumbnails and watermarked image variations.
      A good solution for protecting original images can be found a bit down in this thread:
      Old content of this initial post:
    • By Guy Incognito
      I seem to be running into a repeated fatal error in a fresh PW install version 3.0.148. I can't quite put my finger on the pattern but it seems to be around deleting image fields or removing images from certain image fields.
      This is the trace from the log generated by trying to save a page and delete an image from an image field:
      Fatal Error: Uncaught Error: Cannot access protected property Pageimage::$original in /wire/core/PageimageVariations.php:256 Stack trace: 1. /wire/core/Pageimage.php(1327): PageimageVariations->getInfo() 2. /wire/core/Wire.php(386): Pageimage->___isVariation() 3. /wire/core/WireHooks.php(823): Wire->_callMethod() 4. /wire/core/Wire.php(450): WireHooks->runHooks() 5. /wire/core/Pageimage.php(1369): Wire->__call() 6. /wire/core/Pageimage.php(399): Pageimage->getOriginal() 7. /wire/core/WireData.php(333): Pageimage->get() 8. /wire/core/PageimageVariations.php(256): Pro Line 256 of /wire/core/PageimageVariations.php Earlier in the day I was experimenting with custom fields for images for the first time and kept running into this error, thinking it was me using this feature wrong and not having time to read up I deleted the custom image fields template and went about my business. So now I don't know if I triggered an issue or whether it was never related to the custom image fields in the first place?
      Any ideas?
    • By humanafterall
      I'm using some Custom fields for images: 

      When I save the page, and return the fields are blank. When I re-add the text to the fields and save again then the fields save as expected.

      I know this is stated as being quite experimental but it's super useful feature I'd love to get working correctly.
      I have fields that are CKEditor fields but have overidden this on the image specific template. I've also tried it with regular text fields and I get the same bug.
      (currently using Processwire 3.0.155)
      I've found this issue is specific to editing on pages using the PageTable fieldtype. The fields are not saving when I save the page in the PageTable.
    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.

      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
  • Create New...