How can I exclude current page from perch_content_custom?

  • So I have a page template which has a 'project_details' template within it.


    I then have a 'project_thumb' template that's used with perch_content_custom to create thumbnails from the 'project_details' templates found within pages.


    At the bottom of the page I'm trying to display 3 thumbnails of 'more projects'.


    Is there a way to exclude the current page in the perch_content_custom function?


    Simplified Version of my page template:


    Right now, there's the possibility of having a thumbnail appear for the same page that is currently being viewed and I want to exclude this current page from the pulled pages.

  • Hi Lee,


    You can filter the items with perch_content_custom() to exclude the current one given you know something unique about it (e.g. a slug):


    PHP
    1. perch_content_custom('Projects', [
    2. 'filter' => 'slug', // the field ID of <perch:content id="slug">
    3. 'match' => 'neq', // not equal to
    4. 'value' => 'my-project-slug', // the value of the field with the ID "slug"
    5. ]);
  • Hi Lee


    It's not clear from your template how you are getting the project details on a per page basis, but you can use a filter to exclude the current project from the project listing.


    If you are using a list/detail method by using a slug in the URL, then you can filter the results using the project's slug as below:

    Code
    1. 'filter'=>'project_slug',
    2. 'match'=>'neq',
    3. 'value'=> perch_get('s'),

    Otherwise you would need to use another method to get the id of the current project being displayed and filter on that.

  • 'value' => 'my-project-slug', // the value of the field with the ID "slug"

    Thanks, I understand all that, but I'm having a hard time figuring out how to dynamically fill this part with content pulled from further up the page here:

    PHP
    1. <?php perch_content('Project Details') ?>

    If I was working in JS I would do something like:

    Code
    1. const projectName = document.getElementById('projectName).innerText;

    and then replace the value with the projectName variable.


    Is there anything similar in PHP?

  • I THINK I might have found a workaround by adding this to the page template and setting the template of 'Project Name' to be a code block:

    PHP
    1. <?php
    2. $projectTitle = perch_content('Project Name', true);
    3. ?>

    then adding this to the perch_content_custom function:

    Code
    1. 'value'=> $projectTitle,

    Feels a bit hacky and means adding the project name to the project details section as well as the page individually but it works. I guess the problem is mainly coming from the fact that I was placing all of the project data, and therefore anything I can use to distinguish this page from any other, within the 'Project Details' template rather than directly onto the page template.

  • It is not clear enough to me how you are implementing your list/detail pattern for me to suggest a solution. Are you creating a page for each project instead of relying on a single Region?


    Normally, you would create a multiple-item Region which you would use to output both a list of the items and a single item's details. The single item's view is often determined by the URL e.g. /projects.php?s=my-project-slug or /projects/my-project-slug.



    So you would typically rely on the slug in the URL. Assuming you're using /projects.php?s=my-project-slug, you'd get the slug with perch_get('s'):




    Resources you may want to refer to: