Greetings everyone,
Got an issue that I donāt even know where to start or do
I have a page called āInterestsā that has a structure field where I add multiple āinterestsā in a subject, for example āSportsā.
Then I have a āExperiencesā page with children pages (Each experience).
The previous field is populating a multiselect field in the āExperiencesā children pages, being used as a category system, where I can select multiple āInterestsā.
On my homepage I have an āInterests sectionā where itās displayed each interest āSportsā, etcā¦What i want do is display the number of āexperiencesā that have the interest āSportsā.
Something to displayed like āSports as 10 experiences availableā
But I donāt even know what to do or how to do itā¦any thoughts?
Thank you for your time
You can filter experiences for each interest type, for example
$noOfSportExperiences = page('experiences')->children()->listed()->filterBy('experience', 'sports', ',')->count();
change field name to your field name.
Ok, I understand, but I have multiple interests, meaning I can write them on the structure field.
This is the field being populated inside each āexperienceā:
filterInterest:
label:
en: Choose your interest
type: multiselect
options: query
query:
fetch: site.find('interests').intList.toStructure
text: "{{ structureItem.intName }}"
value: "{{ structureItem.intName.slug }}"
Regarding the field, it has to be the multiselect field right?
Thank you
Sure, but somehow you have to count them, that was just an example for one interest.
If you want to apply multiple interests to a single entry, then you have to use either a multiselect field or a tags field or a checkboxes field.
Great, it worked for the āsportsāā¦but how to get it working with the custom fields being added:
$numberOfInterests = page('experiences')->children()->listed()->filterBy('filterInterest', structureItem.intName, ',')->count();
I tried like this but no luck.
Why do you need the structureitem here? After all, all you need is the interests that are actually used?
How do you output your interests on the homepage? I guess you pluck all entries from the experience pages? Then while looping through this array, you can count the pages for each interest.
Yes, you are right, but how do I get the custom names for the multiselect, thatās the part iām not getting itā¦
And noā¦iām not using pluck
You can get the custom name by using findBy()
on the structure field.
$item = page('interests')->nameOfStructureField()->toStructure()->filter(function($item) use($interest) {
return $item->intName()->slug() === $interest;
})->first();
echo $item ? $item->intName() : $interest;
Where $interest is the sluggified values that is stored in the experience pages.
What then? Probably best if you post your home page code. But Iām going to have some now ā¦
Thank you so much Sonja, iāll have a look first and see if I can get there.
And afterwards iāll add my code here, iām so sorry for this, second website using Kirby and itās been a rideā¦
And we will talk tomorrow, thank you so much for your time.
Ok, iāve been trying to undersatand what iām doing wrong, but i cannot get my head around itā¦
Iām gonna share what i have at the moment:
Yaml for the interests page (I donāt have children pages, only a structure field to add the interests ):
intList:
label: Add your interests
type: structure
fields:
intPhoto:
width: 1/2
type: files
label: Photo
max: 1
multiple: false
info: "{{ file.dimensions }} Pixels • {{ file.size }} Bytes"
intName:
width: 1/2
type: text
icon: title
label: Name
intDesc:
type: textarea
icon: text
size: tiny
buttons: false
label: Small description
Yaml for the experience (Single) page:
filterInterest:
label: Choose your interest
type: multiselect
options: query
query:
fetch: site.find('interests').intList.toStructure
text: "{{ structureItem.intName }}"
value: "{{ structureItem.intName.slug }}"
Interests section on the homepage:
<?php $interests = page('interests')->intList()->toStructure();?>
<?php if ($interests->isNotEmpty()) : ?>
<section class="interests pl-40 pr-40">
<div class="container pt-120 pb-120">
<div class="flex gap-80 justify-between items-center mb-40">
<h2 class="fs-32 bold"><?php echo t('experiences-every-interest') ?></h2>
<a href="interests.html" target="_self" class="bold fs-18"><?php echo t('show-interests') ?> Ā»</a>
</div>
<div class="list grid col-3-13 gap-20 gap-y-40">
<?php $interests->shuffle()->limit(3);
foreach ($interests as $interest) : ?>
<div class="tile">
<a href="#" target="_self" class="mb-20">
<div class="image grid items-center align-center justify-center gradient">
<?php if($interest->intName()->isNotEmpty()) : ?>
<h3 class="light bold fs-48"><?= $interest->intName() ?></h3>
<?php endif; ?>
<?php if ($intCover = $interest->intPhoto()->toFile()): ?>
<img src="<?= $intCover->crop(520, null, 'center center', 85)->url() ?>" alt="<?= $intCover->alt() ?>" title="<?php echo t('courtesy-of') ?> <?= $intCover->photographer() ?> - <?= $intCover->link() ?>">
<?php endif; ?>
</div>
</a>
<div class="flex items-center justify-between mb-20">
<p class="bold">
<?php $numberOfInterests = page('experiences')->children()->listed()->filterBy('filterInterest', 'sports', ',');?>
<?= $numberOfInterests->count() ?>
<?php echo t('experiences') ?></p>
<a class="bold" href="#" target="_self"><?php echo t('show-all') ?> Ā»</a>
</div>
<?php if($interest->intDesc()->isNotEmpty()) : ?>
<div><p><?= $interest->intDesc() ?></p></div>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif; ?>
Since I donāt have interests children pages, do i need to use pluck?
Thank you for the help @texnixe , itās been really helpful.
Any further thoughts on this? Do you think i need to rework any of the code in order to better? use tags instead?
Kind regards
ultimatedabbler:
use tags instead?
I donāt see any reason to use tags.
The advantage of using pluck()
on the children of the experiences field would be that you only get the interests that are really used vs. if you get them from the structure field they might not even have any pages. So depends on use case.
Thank you for the quick reply @pixelijn , Iām not really a good programmer, and never used pluck before, iāll have look into in order to understand how it works.