Skip to content

Instantly share code, notes, and snippets.

@husseinalhammad
Created October 1, 2020 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husseinalhammad/9772da608894e6033a69103cb8051bd9 to your computer and use it in GitHub Desktop.
Save husseinalhammad/9772da608894e6033a69103cb8051bd9 to your computer and use it in GitHub Desktop.
Get similar Perch Collection items
<?php
// get the main item for the detail page
// assuming you're getting the slug from the URL
$item = perch_collection('Case Studies', [
'template' => 'case_studies/_detail',
'skip-template' => true,
'return-html' => true,
'filter' => 'slug',
'value' => perch_get('s'),
]);
// respond with 404 when appropriate
if (!isset($item[0])) {
PerchSystem::use_error_page(404);
exit;
}
// get the category path(s) from the item
// normally you'd be able to use $item[0]['categories'] directly, but Perch sometimes return the category IDs so use pipit_category_get_path()
$categories = [];
if (isset($item[0]['categories'])) {
foreach ($item[0]['categories'] as $cat) {
$categories[] = pipit_category_get_path($cat);
}
}
// output main item
echo $item['html'];
// output similar items
if (!empty($categories)) {
perch_collection('Case Studies', [
'template' => 'case_studies/_list',
'category' => $categories,
'filter' => 'slug',
'match' => 'neq',
'value' => perch_get('s'),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment