Created
October 1, 2020 15:46
-
-
Save husseinalhammad/9772da608894e6033a69103cb8051bd9 to your computer and use it in GitHub Desktop.
Get similar Perch Collection items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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