Forum

Thread tagged as: Question

How to display 'similar projects' based on their category on a detail page

Hi Forum! I may not be searching for the right terms but I cannot find anything about this online so here goes:

I have a list, detail and category page set up for a projects section on a website, all working fine. What I would like to do is display similar projects, i.e. list of projects from the same category at the bottom of the detail page. How do I do this?

Let me know if you need any of my code but as I say it's a pretty bog standard set up but I just can't find what I'm looking for anywhere else.

Emily Taylor

Emily Taylor 0 points

  • 2 years ago

Take a look here...

https://docs.grabaperch.com/perch/categories/filtering/

    perch_content_custom('Portfolio', array(
      'template' => 'portfolio_listing.html',
      'page' => '/portfolio.php',
      'category' => perch_get('cat'),
    ));

Unfortuntely, I've tried that but it's not filtering them out. It's still displaying all categories. Ideally I want it to be able to tell what category the project on the detail page is assigned to and display other projects from the same category below it. I'll have to keep looking. Thanks though!

Emily, how are you populating the category variable?

Posting your code is the best way for us to help you troubleshoot... otherwise were just throwing out ideas...

I'm not sure what you mean by category variable but here's my code.

This is my index page, I've got a list of categories as a subnav across the top.

<?php 
    perch_categories( array(
    'template' => 'category_list.html',
    'set' => 'projects',
    'include-empty' => true,
    )); 

    perch_content_create('Projects', array(
      'template'   => '/projects_content/project.html',
      'multiple'    => true,
      'edit-mode' => 'listdetail',
    ));

    perch_content_custom('Projects', array(
      'template' => '/projects_content/project_listing.html',
      'paginate' => true,
      'count' => 21,
    ));
?>

This is my detail page, this code isn't working. The detail displays fine but the category code below displays all categories instead of the one the project belongs to.

<?php
    perch_content_custom('Projects', array(
      'page' => '/projects/index.php',
      'template' => '/projects_content/project.html',
      'filter' => 'slug',
      'match' => 'eq',
      'value' => perch_get('s'),
      'paginate' => true,
      'count' => 1,
    ));

    perch_content_custom('Projects', array(
      'template' => '/projects_content/project_listing.html',
      'page' => '/projects/index.php',
      'category' => perch_get('cat'),
    ));
?>

This is my category page code. This work fine.

<?php    
if (perch_get('cat')) {
  perch_category(perch_get('cat'),array(
    'template'=>'category_single.html'
));
perch_categories( array(
    'template' => 'category_list.html',
    'set' => 'projects',
    'include-empty' => true,
)); 
perch_content_custom('Projects', array(
  'template' => '/projects_content/project_listing.html',
  'page'=>'/projects/index.php',
  'category' => perch_get('cat'),
));
} else {
  perch_categories();
}
?>

Are you passing the category on the url?

I want to say no because I don't actually know what that means. I'm a super newbie to categories.

EXAMPLE: www.thisIsMySuperDuperDomain.com/myFancyDirectory?cat=myFancyCategoryPassedOnURL

Are you passing category on the url something like this??

Oh hang on, yes then I have: www.thisIsMySuperDuperDomain.com/projects/category?cat=projects/category That's a bit of a weird url right?

The code examples you post are expecting the category to be part of your url... perch_get('cat') will be populated with the value you pass on the URL as ?cat=projects/project1

Ah ok, I think I understand what you are saying. I'm actually trying to pass the category on the detail page which has the url www.thisIsMySuperDuperDomain.com/projects/detail?s=project-name so it isn't going to work on that page because it isn't the category page it's the detail page.

you can add to the URL query string so www.thisIsMySuperDuperDomain.com/projects/detail?s=project-name&&cat=projects/project1

What I was getting at with my original question is perch_get('cat') is expecting to find cat=something on the URL, and if it isn't there you would be getting results your experiencing.

Oh yes, so it's looking at the URL to find which category to pass. I see! Thank you so much for your help, and patience! That change to the url has worked.