Forum
Hide current collection item based on slug?
Hi,
I found a neat perch forum post explaining how to hide the current blog post from a sidebar listing the latest 5 blog posts. Here's the code:
<?php
perch_blog_custom([
'count' => 5,
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'template' => 'sidebar/news--latest.html',
'blog' => 'news',
// Removes the current post from the list
'filter' => 'postSlug',
'match' => 'neq',
'value' => perch_get('s'),
]);
?>
I was wondering if there was a similar technique for doing the same with a perch collection
, I've tried this, but it doesn't work:
<?php
perch_collection('projects', array(
'template' => '_projects/projects--list.html',
'category'=> 'projects/business',
// Is it possible to remove the current collection item from a list?
'filter' => 'project_name_slug',
'match' => 'neq',
'value' => perch_get('s'),
));
?>
Is it possible to hide a collection
item based on the ID project_name_slug
matching the slug
What is the value of
s
on your URL?Hi,
The slug is
my-site.com/business/new-employment-space
<perch:showall />
shows me the value ofproject_name_slug
isnew-employment-space
/new-employment-space
is apage
with acollection
at the bottom of the template, listing projects related to that page. The last part of the pageslug
matches the name of thecollection item
, I was hoping I could filter against that and hide it.When you use
perch_get('s')
you're asking for the item named ass
from the URL. That might be a named segment in a Runway route, or if you're using a query string it'd be the value ofs=
from the query string.Hi,
Thanks for the support Drew, really appreciate it.
Unfortunatley, for this particular problem I couldn't use
named segments
as that would involve adding aroute
to every page. I needed a solution that would work inphp
or a perch template.I figured out a solution.
The template above just says, if the
perch_page_path
contains the valueproject_name_slug
add a class namehide-current-item
In this instance it's not critical the
project-item
is output to the page, I just needed a way to hide a project, if it's name was in theurl
.Could a similar thing be done with
php
, without usingslug
? So theproject-item
doesn't get output.Hello Stephen,
You don't have to use
perch_get()
if you have no use for it. Check the documentation for what it does if you are unsure.If you don't want to make use of Runway's routing, you can get the slug from the URL. You can get the page URL with
perch_page_url()
and then get the last segment of the URL in PHP.Hi,
Thanks, I really appreciate you taking the time to provide an answer.
Is there anyway to just get the last part of the URL?
In this instance the
perch_page_path
is/business/research-and-development
I need to filter on just the
/research-and-development
part of the slug.Also unfortunately, debug shows this error:
Any ideas how I'd get around that?
projects--business.php
The
$slug
variable is commented out and not declared any where else, so that's normal. I left that bit for you to handle.There is more than one way to get the last segment of the URL.
basename()
should do the trick in your case:That's brilliant, thanks for your help :)