Forum

Thread tagged as: Question, Problem

Callback in perch_pages_navigation

I'm having a bit of trouble with passing perch content into a navigation template and was wondering if it is possible to use the callback function in perch_pages_navigation, or if there's another way to modify each page individually?

The problem I'm having is that each one of my pages contains an image uploaded by the user, and I'd like that image to show up when outputting a list of those pages. Because the image is already uploaded into the body of the page I'd rather not use page attributes to add a duplicate copy of the image.

I know how to pass variables into a navigation template, and it works fine when there is one link - the problem occurs when I need to update that variable with the new image path for each page. For example, this is my navigation template:

<perch:before>
<div class="wrapper">
</perch:before>

    <a href="<perch:pages id="pagePath" />"><perch:pages id="pageNavText" /><img src="<perch:pages id="headerImage" />" /></a>

<perch:after>
</div>
</perch:after>

Then on my list page I have something similar to:

$allImages = perch_content_custom('Course Header',array(
  'page' => '/courses/*',
  'skip-template'=> true,
));

for ($i=0; $i < count($allImages); $i++) { 
  PerchSystem::set_var('headerImage', $allImages[$i]['image']);
}

...which obviously sets every image the same (the last in the for loop). Feel like I might be going about this whole thing in the wrong way but I can't think of a way to update the same variable with different contents for use in a template. Any help would be appreciated!

Alan Longstaff

Alan Longstaff 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'd really recommend using page attributes - even if that means selecting the image for a second time. It's going to be really hard to achieve this in a performant way otherwise.

Alright no worries - I'll have a look at moving the images over into attributes, then at least they'll be added once. Unfortunately the system was in place before attributes were introduced and I never got around to switching them over

Thanks!