I’m trying to display all clients testimonials on my home page. Testimonials are by projects, so they are in each individual project page under the form of a structured field (quote, name of the client, role, client picture).
So I tried this:
<?php foreach(page('projects')->children()->pluck('clientTestimonials')->toStructure()->limit(2) as $testimonial): ?>
and have the error “Fatal error: Call to a member function children() on boolean in /templates/home.php on line 274”
Then I tried this:
<?php foreach(page('projects')->children->clientTestimonials()->toStructure()->limit(2) as $testimonial): ?>
with non luck either
Same error message as above + “Notice: Trying to get property of non-object in /templates/home.php on line 274”
Does a structured field can be plucked ? How can I do what i want to do ? With 2 foreach ?
<?php
$projects = page('project')->children()->visible();
$testimonials = new Structure();
foreach($projects as $p) {
$testimonials = $testimonials->merge($p);
}
foreach($testimonials as $testimonial):
?>
with no success and this error message:
Notice: Undefined property: Page::$data in /kirby/toolkit/lib/collection.php on line 40
Warning: Invalid argument supplied for foreach() in /kirby/toolkit/lib/a.php on line 463
first problem: <?php foreach ($testimonials as $testimonial): ?> gives me all the testimonials “plucked” and it seems that i can’t chain ->shuffle() or ->limit(3) to it (in your example you can sort it and chain easily). Is this normal ?
Second problem: <?php echo $testimonial->clientTestimonial()->kt() ?> echoes the clientTestimonial field (great) but all the others fields echo the same thing, for example <?php echo $testimonial->clientTestimonialName() ?> also echoes clientTestimonial instead of the right field and I can’t find why.
any ideas ?
//Edit : in fact the first testimonial is empty, the second is just the quote repeated, the third is just the name, the fourth is just the role etc… I’m going to investigate.
//Edit 2: in fact it’s repeating only one of the items over and over.
<?php
$projects = page('projets')->children()->visible();
$testimonials = new Structure();
$key = 0;
foreach($projects as $p) {
foreach($p->clientTestimonials()->toStructure() as $item) {
$testimonials->append($key, $item);
$key++;
}
}
foreach ($testimonials as $testimonial): ?>
<!--some html ul li for repeated item-->
<blockquote>
<?php echo $testimonial->clientTestimonial()->kt() ?>
</blockquote>
<p><strong><?php echo $testimonial->clientTestimonialName() ?></strong><span><?php echo $testimonial->clientTestimonialRole() ?></span></p>
<!--end of html block-->
<?php endforeach ?>
and the datas:
----
clientTestimonials:
-
clientTestimonial: Vestibulum quam nisi, pretium a nibh sit amet, consectetur hendrerit mi. Aenean imperdiet lacus sit amet elit porta, et malesuada erat bibendum.
clientTestimonialName: Jean Dupont-avec-un-nom-très-long
clientTestimonialRole: Gestionnaire de syndic
clientTestimonialAvatar: rien
-
clientTestimonial: Vestibulum quam nisi, pretium a nibh sit amet, consectetur hendrerit mi.
clientTestimonialName: Miriam Jonhson
clientTestimonialRole: Gardienne
clientTestimonialAvatar: rien
-
clientTestimonial: Aenean imperdiet lacus sit amet elit porta, et malesuada erat bibendum.
clientTestimonialName: Robert Di Caprio
clientTestimonialRole: Chef de chantier
clientTestimonialAvatar: rien
----
Hm, I’m very sorry, I can’t spot anything wrong here; I have the same sort of structure in one of my projects and use the same code without any problems …
Ok, any idea on how I can shuffle it with vanilla php ? And why when I shuffle on the first collection page('projets')->children()->visible()->shuffle I always end with the same result ?
I had this idea too but I didn’t know how to implement it. Where can I put the yaml ?
Also I don’t understand why when I shuffle the first pages collection it doesn’t change anything, at least the testimonials had to be shuffled by page.
The content would be unchanged, but instead of using toStructure in your template you would use yaml. It returns a plain PHP array that can be manipulated with the standard PHP functions.
You would also use a plain array for $testimonials instead of a new Structure(). You can add items to an array with $testimonials[] = $item.