Jump to content

{solved} find a field from parent pages by ID


neophron
 Share

Recommended Posts

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

2 hours ago, elabx said:
if($item->matches('parent=1223')){ ... }

Maybe try this?

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 ?>

 

error.jpg

Link to comment
Share on other sites

7 minutes ago, elabx said:

I might have complicated this way tooe much maybe simply:

$item->parent->id == 1234

 

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

44 minutes ago, elabx said:
<?php foreach ($pages->findIDs('id=1223|1224|1225, sort=-created')->children as $item) : ?>

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

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

  • Like 2
Link to comment
Share on other sites

<?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>

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

26 minutes ago, 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>

 

Thanks! Now it works fine ?

I'm going to have a  look at this »Latte«

  • Like 2
Link to comment
Share on other sites

  • neophron changed the title to {solved} find a field from parent pages by ID

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

Your logic is impossible.

1 hour ago, neophron said:

Find all children from parent=1223|1224|1225 and echo a teaser only if they are children from archive

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

35 minutes ago, bernhard said:

Your logic is impossible.

If the page is a child of archive it can not be a child of the other pages

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

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...