Forum
Get a setting into perch_collection's 'each' callback
How do I pass a setting into the 'each' callback of perch_content_custom?
$currentCat = getThemeCat();
$doThat = true;
$items = perch_collection('Beispiele', [
'skip-template' => true,
'category' => $currentCat,
'sort'=>'projectTitle',
),
'each' => function($item) {
if(!empty($doThat)){
// do that
}
else {
// do this
}
},
],true);
The part that's not working is the if(!empty($doThat))
bit, probably because $doThat is not in the scope of the 'each' callback.
Is there a recommended way to pass $doThat into the callback?
Got it! The callback is called a closure, and it can access variables outside it's scope with the "use" keyword:
https://php.net/manual/en/functions.anonymous.php
And then it turned out I don't really need to do that :-)