Forum

Thread tagged as: Question

Displaying first Image from repeaters on parent page.

I have a repeater on subpages. Each repeater has an image.

Is it possible to output the first image from each subpage to its parent page?

James Curran

James Curran 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can wrap the repeater's contents in:

<perch:if exists="perch_item_first">
...
</perch:if>

Thanks Drew. How do I then output it from the parent?

Here's what I have:

Parent template:

            <?php
                perch_pages_navigation(array(
                        'template' => array('groups.html'),
                        'from-path' => '/gallery',
                        'levels'    => 2,
                ));
            ?>

/templates/navigation/groups.html

    <li>
        <a href="<perch:pages id="pagePath" />">
            <perch:pages id="pageNavText" />
            <perch:content id="gallery_image" type="image" label="Gallery image" width="232" height="150" crop="true" suppress="true"/>
        </a>
    </li>

Subpages template:

<perch:repeater id="images" label="Images">
    <perch:before>
    <ul>
      </perch:before>

      <li>
        <img src="<perch:content id="gallery_image" type="image" label="Gallery image" width="232" height="150" crop="true"  />" />
      </li>

    <perch:after>
    </ul>
    </perch:after>
</perch:repeater>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have your repeater showing on the page where you want it?

Yes the repeater is for a gallery of images on each subpage and that works.

What I'm trying to do is show the first image on the parent page using perch_pages_navigation

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it showing on the parent page?

I'm unclear whether your issue is displaying the repeater, or displaying just the first item.

Just the first item.

Each subpage has a repeater (header and image).

I want the parent to display a list made up from each subpage's first repeater item.

I know I can add images for nav with attributes, but I was trying to remove that step for editors, so that it just picks up the first image added to the list on each new subpage.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you created a template to show just the first item as per my initial reply?

Yes I can see that I'm going about it the wrong way though. Tried this:

templates/content/gallery:

<perch:repeater id="images" label="Images">
    <perch:before>
    <ul id="lightgallery" class="list-unstyled row" >
      </perch:before>
         <perch:if exists="perch_item_first"> 
                 <li> 
                <img src="<perch:content id="gallery_first_image" type="image" label="Gallery image" width="232" height="150" crop="true"  />" />
                 </li>
         <perch:else />
                 <li> 
                <img src="<perch:content id="gallery_image" type="image" label="Gallery image" width="232" height="150" crop="true"  />" />
                 </li>
         </perch:if>
    <perch:after>
    </ul>
    </perch:after>
</perch:repeater>

Then templates/navigation/galleries:

<li>
    <a href="<perch:pages id="pagePath" />" >
        <img src="<perch:pages type="image" id="gallery_first_image" width="232" height="150" crop="true" />"/> 
        <perch:pages id="pageNavText" />
    </a>
</li>

Then:

<?php
    perch_pages_navigation(array(
        'from-path' => '/gallery',
        'levels'    => 1,
            'template' => 'galleries',
    ));
?>

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is the content in page attributes or in a content region?

A content region.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't access content regions using navigation functions. You'll need to use perch_content_custom() to display the region.

Can perch_content_custom() still get the first item from each new subpage that is created in the same way as perch_pages_navigation?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can use the wildcard syntax for the page option - it's in the documentation.

Ok thanks for all your help, I'll check it out.