Forum

Thread tagged as: Question

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

Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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 of project_name_slug is new-employment-space

/new-employment-space is a page with a collection at the bottom of the template, listing projects related to that page. The last part of the page slug matches the name of the collection item, I was hoping I could filter against that and hide it.

Drew McLellan

Drew McLellan 2638 points
Perch Support

When you use perch_get('s') you're asking for the item named as s 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 of s= 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 a route to every page. I needed a solution that would work in php or a perch template.

I figured out a solution.

<div class="project-item <perch:if id="perch_page_path" match="contains" value="{project_name_slug}">hide-current-item</perch:if>"> [template content]</div>

The template above just says, if the perch_page_path contains the value project_name_slug add a class name hide-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 the url.

Could a similar thing be done with php, without using slug ? So the project-item doesn't get output.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

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.

Unfortunatley, for this particular problem I couldn't use named segments as that would involve adding a route to every page. I needed a solution that would work in php or a perch template.

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.

$page_url = perch_page_url([
    'include-domain' => false,
], true);

// $slug = get from $page_url

perch_collection('projects', array(
'template' => '_projects/projects--list.html',
'category'=> 'projects/business', 
'filter' => 'project_name_slug',
'match' => 'neq',
'value' => $slug 
)); 

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:

Array
(
    [type] => 8
    [message] => Undefined variable: slug
    [file] => /Users/stephen/Repositories/my-site/cms/perch/templates/layouts/projects/projects--business.php
    [line] => 14
)

Any ideas how I'd get around that?

projects--business.php

<?php 
$page_url = perch_page_url([
'include-domain' => false,
], true);

// $slug = get from $page_url

echo "$page_url"; //Tests $page_url is working, it is. Outputs: /business/research-and-development

perch_collection('projects', array(
'template' => '_projects/projects--list.html',
'category'=> 'projects/business', 
'filter' => 'project_name_slug',
'match' => 'neq',
'value' => $slug
)); 

?>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Also unfortunately, debug shows this error

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:

$page_url = perch_page_url([
'include-domain' => false,
], true);

$slug = basename($page_url);

That's brilliant, thanks for your help :)