Can't get match within if statement

Hey all,

Weird issue here:

foreach(page('testimonials')->contentTestimonials()->toStructure() as $testimonial) {
    if($testimonial->author()->html() == $page->metaTestimonial()->html()) {
      echo 'match';
    }
  }

Basically all the correct information is being pulled down, the but in the if statement I can’t get a match. Replacing either side of the if statement with John Doe, CEO Acme Ltd. makes a match when on the correct page but like it is I can’t get a match.

Appreciate any help I can get.

Cheers

Compare like this: html($testimonial->author()) == html($page->metaTestimonial()).

Using $page->title()->html() will return an Object, html($page->title()) will return a string.

You sir are a genius!

Many thanks

This should also work, value() returns the content of a field object as string:

foreach(page('testimonials')->contentTestimonials()->toStructure() as $testimonial) {
    if($testimonial->author()->value() == $page->metaTestimonial()->value()) {
      echo 'match';
    }
  }
3 Likes

I agree with texnixe. When accessing fields in code, I’m usually interested in the “raw” value, not a processed form of the content.

Yes, works perfectly, cheers!

Looks a bit tidier also I think…

There’s a typo in your code in the second value I think. This fixed it:

foreach(page('testimonials')->contentTestimonials()->toStructure() as $testimonial) {
    if($testimonial->author()->value() == $page->metaTestimonial()->value()) {
      echo 'match';
    }
  }

Oh, yes, thanks, I corrected it above.