Forum
Display 404 Page in Runway if route matches but no content
I'm looking to force show my custom 404 page in Runway if route matches but there is no content for a collection item. Currently, we just get a templated page with no content.
Using PerchSystem::use_error_page(404); has been mentioned before but there doesn't appear to be anything documented for use with Collections.
How do I perform the check correctly and redirect to the 404 if there is no post? This is what I have so far - but it doesn't redirect to my 404 when testing - it embeds the 404 template in my post template.
# get the post
$post = perch_collection('News', [
'filter' => 'titleSlug',
'match' => 'eq',
'value' => perch_get('s'),
'skip-template' => 'true',
'return-html' => 'true',
]);
if (empty(array_filter($post))) {
PerchSystem::use_error_page(404);
} else {
echo $post['html'];
}
Make sure you call
PerchSystem::use_error_page
before outputting anything else.Ah super - that makes sense - so wrap the template in the if statement.