Forum

Thread tagged as: Question

Multiple Regions in perch_content_custom()

Is it possible to pull in content from multiple regions using perch_content_custom?

I have a set of pages with regions called "logo" and "opening hours". I'd like to output the content of those regions from each of those pages into a set of blocks on another page.

Now Media

Now Media 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you can pass an array of region names in.

perch_content_custom(array('logo','opening hours'), array(...));

Awesome. All working apart from the Opening Hours is a multi-item region (not a repeater, d'oh). How do I get it to pull in more than one item?

<div class="caption cf" data-caption="bhs">
    <div class="caption__logo">
        <img src="<perch:content id="image" type="image" label="Upload" bucket="store-logos" />" alt="<perch:content id="alt" type="text" label="Shop Name" help="eg. Boots" required="true" />">
    </div>
    <div class="caption__bd">
        <h2 class="hours-title">Shopping Hours</h2>
        <p><perch:content id="days" type="text" label="Day(s)" /><br>
        <perch:content id="hours" type="text" label="Hours" /></p>
    </div>
 </div>

Pulls in just the first set of hours

<perch:before>
<div class="caption cf" data-caption="bhs">
    <div class="caption__logo">
        <img src="<perch:content id="image" type="image" label="Upload" bucket="store-logos" />" alt="<perch:content id="alt" type="text" label="Shop Name" help="eg. Boots" required="true" />">
    </div>
    <div class="caption__bd">
        <h2 class="hours-title">Shopping Hours</h2>
</perch:before>
        <p><perch:content id="days" type="text" label="Day(s)" /><br>
        <perch:content id="hours" type="text" label="Hours" /></p>
<perch:after>
    </div>
 </div>
</perch:after>

Pulls in every shop's hours in one bulk

From what I can see, by putting

 <perch:showall />

it is outputting the two regions separately

Drew McLellan

Drew McLellan 2638 points
Perch Support

What output do you get? I don't know how your data is structured, so I'm working blind.

Some example data exported vie <perch:showall />

Perch Data

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so that looks like 3 items from opening-hours and one from logo.

What were you expecting?

Kind of. But the whole list seems to come through in a bit of an erratic order. I get the logo then one of the hours, then the rest of the hours corresponding to that shop will be much further down the list

What I was kind of after, was the logo and hours to be grouped together per "shop page". As my desired effect was to create a panel with the shop logo and their opening hours for each shop, all on one page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok - how are you ordering them?

I'm guessing I need a field that is common to both regions, so would "_pageID" work?

Nope, "_pageID" doesn't work. showall spits out nothing

 <?php perch_content_custom(array('Logo','Shopping Hours'),array(
        'page' => '/store-directory/*',
        'template'=>'map_caption.html',
        'sort'=>'_pageID',
        'sort-order'=>'ASC',
        'sort-type' => 'numeric'
    ));             
?>

Drew McLellan

Drew McLellan 2638 points
Perch Support

It needs to be common to both but also put them in the correct order.

I have added the alt field to the opening hours region. I have also converted the opening hours region to include a repeater. I am now sorting the data by 'alt'

<?php perch_content_custom(array('Logo','Shopping Hours'),array(
        'page' => '/store-directory/*',
        'template'=>'map_caption.html',
        'sort' => 'alt',
        'sort-order'=>'ASC'                             
     ));                
?>

This is an example is the data I get now

Perch Data

<div class="caption cf" data-caption="">
    <div class="caption__logo">
        <img src="<perch:content id="image" type="image" label="Upload" bucket="store-logos" />" alt="<perch:content id="alt" type="text" label="Shop Name" help="eg. Boots" required="true" />">
    </div>
    <div class="caption__bd">
        <h2 class="hours-title">Shopping Hours</h2>
        <perch:repeater id="opening-hours" label="Opening Hours">
            <p><perch:content id="days" type="text" label="Day(s)" /><br>
            <perch:content id="hours" type="text" label="Hours" /></p>
        </perch:repeater>
    </div>
 </div>

Whilst the template displayed above is now getting all the data it needs, it is creating two code block. One that has the logo and no hours, and one that has the hours and no logo

It's like it is making two passes through the template, one for each region.

Excuse the rather messy PHP code (it's not my first skill). I cracked it via the raw data method.


$output = ""; $logos = perch_content_custom(array('Logo'),array( 'page' => '/store-directory/*', 'skip-template' => true )); $hours = perch_content_custom(array('Shopping Hours'),array( 'page' => '/store-directory/*', 'skip-template' => true )); $newCaptions = array(); foreach($logos as $logo) { $thisPageID = $logo['_pageID']; $output .= "<div class=\"caption cf\" data-caption=\"".$logo['alt']."\">"; $output .= "<div class=\"caption__logo\">"; $output .= "<img src=\"".$logo['image']."\" alt=\"".$logo['alt']."\">"; $output .= "</div>"; $output .= "<div class=\"caption__bd\">"; $output .= "<h2 class=\"hours-title\">Shopping Hours</h2>"; foreach($hours as $hour){ if($hour['_pageID'] === $thisPageID){ foreach($hour['opening-hours'] as $thisHour){ $output .= " <p>".$thisHour['days']."<br>"; $output .= $thisHour['hours']."</p>"; } } } $output .= "</div>"; $output .= "</div>"; }