Jump to content

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


LedZepmann
 Share

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!

Thanks,

Dave

Link to comment
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}' />";

Success!

  • Like 1
Link to comment
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');

Edit:

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

Edited by kongondo
  • Like 5
Link to comment
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
Link to comment
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
Link to comment
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
 Share

×
×
  • Create New...