neophron Posted May 26, 2022 Share Posted May 26, 2022 Hi guys, I'm struggling with the following situation: Structure: parent_page 01 (template01) --> shows teaser from child pages child-page 01 child-page 01 child-page 01 parent_page 02 (template01) --> shows teaser from child pages child-page 02 child-page 02 child-page 02 parent_page 03 (template02) --> shows teaser from child pages child-page 03 child-page 03 child-page 03 collect_page (template_collect) --> //This page shows teasers from all child pages In the template01 and 02 is a select options field (select_color) for 3 colors. The child pages gets the parent color by: $page->parent->select_color->title; Now I need the parent colors for all teasers on the collect_page. Something like:<?php foreach ($pages->findIDs('1223|1224|1225, sort=-created')->children as $item) : ?> <?php if item is child page from parent with ID=1223 -> select_color->title : ?> <?php elseif item is child page from parent with ID=1224 -> select_color->title : ?> <?php elseif item is child page from parent with ID=1225 -> select_color->title : ?> Link to comment Share on other sites More sharing options...
elabx Posted May 26, 2022 Share Posted May 26, 2022 if($item->matches('parent=1223')){ ... } Maybe try this? Link to comment Share on other sites More sharing options...
neophron Posted May 26, 2022 Author Share Posted May 26, 2022 On 5/26/2022 at 1:41 PM, elabx said: if($item->matches('parent=1223')){ ... } Maybe try this? Expand Thanks for the hint. I created this, but it gives throws me an error: <?php foreach ($pages->findIDs('1223|1224|1225, sort=-created')->children as $item) : ?> <?php if ($item->matches('parent=1223')) : ?> <a href='<?= $item->url; ?>' class='teaser-angebot-wrapper <?= $item->select_color->title; ?>'> <section> <h3><?= $item->title; ?></h3> <p>Ausstattung: <?= $item->textfield_01; ?></p> </section> </a> <?php elseif ($item->matches('parent=1224')) : ?> <a href='<?= $item->url; ?>' class='teaser-angebot-wrapper <?= $item->select_color->title; ?>'> <section> <h3><?= $item->title; ?></h3> <p>Wo / Wann: <?= $item->textfield_02; ?></p> </section> </a> <?php endif ?> <?php endforeach ?> Link to comment Share on other sites More sharing options...
elabx Posted May 26, 2022 Share Posted May 26, 2022 I might have complicated this way tooe much maybe simply: $item->parent->id == 1234 Link to comment Share on other sites More sharing options...
neophron Posted May 26, 2022 Author Share Posted May 26, 2022 On 5/26/2022 at 5:17 PM, elabx said: I might have complicated this way tooe much maybe simply: $item->parent->id == 1234 Expand I'm still getting an error. I this think, that this line causes trouble <?php foreach ($pages->findIDs('1223|1224|1225, sort=-created')->children as $item) : ?> Link to comment Share on other sites More sharing options...
elabx Posted May 26, 2022 Share Posted May 26, 2022 Oh yes, fix the ids selector! <?php foreach ($pages->findIDs('id=1223|1224|1225, sort=-created')->children as $item) : ?> 1 Link to comment Share on other sites More sharing options...
neophron Posted May 26, 2022 Author Share Posted May 26, 2022 On 5/26/2022 at 5:30 PM, elabx said: <?php foreach ($pages->findIDs('id=1223|1224|1225, sort=-created')->children as $item) : ?> Expand Thanks, but now I'm getting this error from this line: Trying to get property 'children' of non-object Link to comment Share on other sites More sharing options...
Gideon So Posted May 27, 2022 Share Posted May 27, 2022 Hi @neophron I think the findIDs function only returns the IDs of the found pages but not full wire page objects. https://processwire.com/api/ref/pages/find-i-ds/ You should try using: <?php foreach ($pages->findMany('id=1223|1224|1225, sort=-created')->children as $item) : ?> or <?php foreach ($pages->find('id=1223|1224|1225, sort=-created')->children as $item) : ?> Gideon 2 Link to comment Share on other sites More sharing options...
elabx Posted May 27, 2022 Share Posted May 27, 2022 On 5/27/2022 at 2:58 AM, Gideon So said: I think the findIDs function only returns the IDs pf the found pages but not full wire page objects. Expand Oh! That's gotta be it! Link to comment Share on other sites More sharing options...
bernhard Posted May 27, 2022 Share Posted May 27, 2022 <?php foreach($pages->find("parent=1223|1224|1225") as $teaser): ?> <a href='<?= $teaser->url; ?>' class='teaser-angebot-wrapper <?= $teaser->parent->select_color->title; ?>'> <section> <h3><?= $teaser->title; ?></h3> <?php if($teaser->parent->id == 1223): ?> <p>Ausstattung: <?= $teaser->textfield_01; ?></p> <?php else: ?> <p>Wo / Wann: <?= $teaser->textfield_02; ?></p> <?php endif; ?> </section> </a> <?php endforeach; ?> I've recently become a huge fan of the latte template engine (by the folks that made tracydebugger) as it produces so much cleaner output in your templates and that would be a perfect example: <a n:foreach="$pages->find('parent=1223|1224|1225') as $teaser" class='teaser-angebot-wrapper {$teaser->parent->select_color->title}' href='{$teaser->url}' > <section> <h3>{$teaser->title}</h3> <p n:if="$teaser->parent->id == 1223">Ausstattung: {$teaser->textfield_01}</p> <p n:if="$teaser->parent->id == 1224">Wo / Wann: {$teaser->textfield_02}</p> </section> </a> 1 1 Link to comment Share on other sites More sharing options...
neophron Posted May 27, 2022 Author Share Posted May 27, 2022 On 5/27/2022 at 9:05 AM, bernhard said: <?php foreach($pages->find("parent=1223|1224|1225") as $teaser): ?> <a href='<?= $teaser->url; ?>' class='teaser-angebot-wrapper <?= $teaser->parent->select_color->title; ?>'> <section> <h3><?= $teaser->title; ?></h3> <?php if($teaser->parent->id == 1223): ?> <p>Ausstattung: <?= $teaser->textfield_01; ?></p> <?php else: ?> <p>Wo / Wann: <?= $teaser->textfield_02; ?></p> <?php endif; ?> </section> </a> <?php endforeach; ?> I've recently become a huge fan of the latte template engine (by the folks that made tracydebugger) as it produces so much cleaner output in your templates and that would be a perfect example: <a n:foreach="$pages->find('parent=1223|1224|1225') as $teaser" class='teaser-angebot-wrapper {$teaser->parent->select_color->title}' href='{$teaser->url}' > <section> <h3>{$teaser->title}</h3> <p n:if="$teaser->parent->id == 1223">Ausstattung: {$teaser->textfield_01}</p> <p n:if="$teaser->parent->id == 1224">Wo / Wann: {$teaser->textfield_02}</p> </section> </a> Expand Thanks! Now it works fine ? I'm going to have a look at this »Latte« 2 Link to comment Share on other sites More sharing options...
neophron Posted June 1, 2022 Author Share Posted June 1, 2022 Hi there, meanwhile the client had a new wish and I need your help again. An archive page shall contain children, which will be moved there manually. Structure:archive (archive) --> shows teaser from child pages child-page 02 child-page 01 child-page 03 child-page 01 child-page 01 child-page 02 The classical way to echo child pages is by: <?php foreach ($page->children() as $teaser) : ?> But I need again the data from the color_field mentioned above <?= $teaser->parent->select_color->title; ?> The logic now would be something like this: Find all children from parent=1223|1224|1225 and echo a teaser only if they are children from archive Link to comment Share on other sites More sharing options...
bernhard Posted June 1, 2022 Share Posted June 1, 2022 Your logic is impossible. On 6/1/2022 at 11:32 AM, neophron said: Find all children from parent=1223|1224|1225 and echo a teaser only if they are children from archive Expand If the page is a child of archive it can not be a child of the other pages... I guess you need another approach but I don't really understand what you (or your client) wants, so maybe you try to ask again with other words and a detailed description? Link to comment Share on other sites More sharing options...
neophron Posted June 1, 2022 Author Share Posted June 1, 2022 On 6/1/2022 at 12:35 PM, bernhard said: Your logic is impossible. If the page is a child of archive it can not be a child of the other pages Expand You're absolutely right. I wrote my question too hasty. I'm gonna find a different way for applying a color to a teaser. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now