Forum

Thread tagged as: Question, Problem

Get image from different region perch_content_custom

Hi, I have a page where i'm listing items using perch_content_custom and using a template, i'm also trying to pull in an image from another region and use it in the template via set_vars. But I can't figure out how to get the correct image for each item, at the moment its just outputting the same image for all the items.

I can output the correct info via a foreach loop but can't translate that into vars and use in the template. I know one thing I could do is use perch_content_custom( ['Event Details', 'Page Banner'] [...] ) but then you have to have the 'date' and 'featured' filter in both regions which i want to try avoid, I'll attach all templates, code and debug for the page.

Thank you.

perch_content_custom:

<?php

      $banner = perch_content_custom('Page Banner', [
        'page'=>'/get-involved/events/*',
        'skip-template'=>true,
      ]);

      foreach ($banner as $image) {
        PerchSystem::set_vars([
          'banner' => $image['banner_image'],
        ]);
      }

      // print_r($banner);

        perch_content_custom('Event Details', [
          'page'=>'/get-involved/events/*',
          'template'=>'event_list.html',
          'filter'=>[
            [
              'filter' => 'date',
              'match' => 'gte',
              'value' => date('Y-m-d 00:00:00',strtotime('yesterday')),
            ],
            [
              'filter' => 'featured',
              'match' => 'eq',
              'value' => 'no',
            ]
          ],
          'sort'=>'date',
          'sort-order'=>'DESC',
          'each' => function($item) {
            $item['page_title'] = perch_page_attribute('pageTitle', array(
            '_id' => $item['_pageID'],
            ), true);
            return $item;
          },
          'count'=>10
        ]);
      ?>

event_list.html:

<a href="<perch:content id="_page" />" class="event--listing-event <perch:categories id="categories" set="events"><perch:category id="catSlug" type="slug" /></perch:categories>">
  <img src="<perch:content id="banner" />" />
  <div class="event--listing-event-copy">
    <perch:content id="page_title" />

    <span class="event--listing-event-hover">Sign up</span>
  </div>
</a>

Debug:

Debug Message - Perch 3.1.1
[30] SELECT DISTINCT settingID, settingValue FROM perch3_settings WHERE userID=0
[1] SELECT * FROM perch3_pages WHERE pagePath='/get-involved/events.php' LIMIT 1
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
[1] SELECT * FROM perch3_blog_posts WHERE postStatus='Published' AND postDateTime<='2018-07-09 10:38:00' AND postSlug=''
no post
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
no post
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
no post
[9] SELECT regionKey, regionHTML FROM perch3_content_regions WHERE regionPage='/get-involved/events.php' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch3_pages WHERE pagePath='/how-we-help' OR pageSortPath='/how-we-help' LIMIT 1
[10] SELECT * FROM perch3_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch3_pages WHERE pagePath='/get-involved/events.php' LIMIT 1
[2] SELECT pageID FROM perch3_pages WHERE pageTreePosition IN ('000-003-001', '000-003', '000') ORDER BY pageTreePosition DESC
[9] Using template: /templates/navigation/item.html
[1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch3_pages WHERE pagePath='/get-involved' OR pageSortPath='/get-involved' LIMIT 1
[17] SELECT * FROM perch3_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-003%' ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch3_pages WHERE pagePath='/get-involved/events.php' LIMIT 1
[2] SELECT pageID FROM perch3_pages WHERE pageTreePosition IN ('000-003-001', '000-003', '000') ORDER BY pageTreePosition DESC
[3] Using template: /templates/navigation/item.html
Using template: /templates/search/search-form.html
[12] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE (regionKey='Event Details' OR regionKey='Page Banner') AND (regionPage LIKE '/get-involved/events/%' OR regionPage='*')
[nil] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='date' WHERE ((idx.regionID=171 AND idx.itemRev=2) OR (idx.regionID=173 AND idx.itemRev=3) OR (idx.regionID=176 AND idx.itemRev=1) OR (idx.regionID=178 AND idx.itemRev=2) OR (idx.regionID=186 AND idx.itemRev=2) OR (idx.regionID=188 AND idx.itemRev=6) OR (idx.regionID=158 AND idx.itemRev=4) OR (idx.regionID=160 AND idx.itemRev=2) OR (idx.regionID=166 AND idx.itemRev=2) OR (idx.regionID=168 AND idx.itemRev=2) OR (idx.regionID=181 AND idx.itemRev=2) OR (idx.regionID=183 AND idx.itemRev=3)) AND ((idx.indexKey='date' AND idx.indexValue >= '2018-07-08 00:00:00') OR (idx.indexKey='featured' AND idx.indexValue='yes')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID HAVING count(*)=2 ORDER BY sortval DESC LIMIT 0, 1
Using template: /templates/content/event_spotlight.html
[6] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='Page Banner' AND (regionPage LIKE '/get-involved/events/%' OR regionPage='*')
[6] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=160 AND idx.itemRev=2) OR (idx.regionID=168 AND idx.itemRev=2) OR (idx.regionID=173 AND idx.itemRev=3) OR (idx.regionID=178 AND idx.itemRev=2) OR (idx.regionID=183 AND idx.itemRev=3) OR (idx.regionID=188 AND idx.itemRev=6)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
[6] Using template: /templates/content/page_banner.html
[6] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='Event Details' AND (regionPage LIKE '/get-involved/events/%' OR regionPage='*')
[6] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='date' WHERE ((idx.regionID=158 AND idx.itemRev=4) OR (idx.regionID=166 AND idx.itemRev=2) OR (idx.regionID=171 AND idx.itemRev=2) OR (idx.regionID=176 AND idx.itemRev=1) OR (idx.regionID=181 AND idx.itemRev=2) OR (idx.regionID=186 AND idx.itemRev=2)) AND ((idx.indexKey='date' AND idx.indexValue >= '2018-07-08 00:00:00') OR (idx.indexKey='featured' AND idx.indexValue='no')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID HAVING count(*)=2 ORDER BY sortval DESC LIMIT 0, 10
[1] SELECT * FROM perch3_pages WHERE pageID=48 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID=49 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID=44 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID=47 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID=50 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID=46 LIMIT 1
[6] Using template: /templates/content/event_list.html
[1] SELECT * FROM perch3_categories ORDER BY catTreePosition ASC
[1] SELECT groupID FROM perch3_navigation WHERE groupSlug='footer' LIMIT 1
[1] SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes FROM perch3_navigation_pages np, perch3_pages p WHERE p.pageID=np.pageID AND np.groupID=1 AND p.pageNew=0 AND np.pageDepth >=0 AND np.pageDepth<=1 ORDER BY np.pageTreePosition ASC
[0] SELECT np.pageTreePosition FROM perch3_pages p, perch3_navigation_pages np WHERE np.pageID=p.pageID AND np.groupID=1 AND p.pagePath='/get-involved/events.php' LIMIT 1
[1] Using template: /templates/navigation/footer.html
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='Annual Report' AND (regionPage='/get-involved/events.php' OR regionPage='*')
[1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=162 AND idx.itemRev=2)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
[1] Using template: /templates/content/annual_report.html
[1] SELECT groupID FROM perch3_navigation WHERE groupSlug='legal' LIMIT 1
[2] SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes FROM perch3_navigation_pages np, perch3_pages p WHERE p.pageID=np.pageID AND np.groupID=2 AND p.pageNew=0 AND np.pageDepth >=0 AND np.pageDepth<=1 ORDER BY np.pageTreePosition ASC
[0] SELECT np.pageTreePosition FROM perch3_pages p, perch3_navigation_pages np WHERE np.pageID=p.pageID AND np.groupID=2 AND p.pagePath='/get-involved/events.php' LIMIT 1
[2] Using template: /templates/navigation/legal.html
ewe agency

ewe agency 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Each time you call PerchSystem::set_vars() you're replacing the banner var with something new.

Instead of looping through here, make it part of your existing each callback.

Okay is it something like this that i need to do? : https://forum.grabaperch.com/forum/05-20-2015-perch-2-creating-related-content-using-each

I tried implementing that in my each but i'm getting an array to string conversion error so i don't think its looping through correctly

'each' => function($item) {
            $item['page_title'] = perch_page_attribute('pageTitle', array(
            '_id' => $item['_pageID'],
            ), true);

            $i = 0;

            foreach($item as $image) {
              $banner = perch_content_custom('Page Banner', array(
                'page'=>'/get-involved/events/*',
                'skip-template' => 'true',
              ));

              $item['banner'][$i]['banner_image'] = $banner[0]['banner_image'];

              $i++;
            }

            return $item;

          },
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is it one banner image you're trying to get, or one image per event?

One image per event.

So under "/get-involved/events/" i have event-1.php, event-2.php, event-3.php etc and they all have different banner images that i'm trying to pull through into the event_list.html template

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so that looks like it might be on the right track. What's the exact error you get, and what's the link of code is corresponds to?

I get the following error in perch debug:

Array
(
    [type] => 8
    [message] => Array to string conversion
    [file] => /perch/core/lib/PerchTemplate.class.php
    [line] => 335
)

and the img source displays as: src="Array"

Drew McLellan

Drew McLellan 2638 points
Perch Support

If you inspect the contents of $banner[0]['banner_image'] is it an array? If so, which key has the image path?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Alternatively, in your template are you using type="image" ?

Attitude is a company of video storytellers who get to the heart of people's lives. https://mirchistatus.com/videostatus/Gujarati+Status/new2old/1.html