Forum

Thread tagged as: Question, Blog

Perch Categories (related posts) query

Trying to get a list of posts related to the currently shown post. This is done by categories and must factor in multiple categories if present.

category_only.html:

<perch:category id="catSlug" type="slug" for="catTitle" /><perch:if exists="perch_item_last"><perch:else />|</perch:if>

post.php

<?php
  $cats = perch_blog_post_categories(perch_get('s'), array(
    'template'=>'category_only.html',                             
  ), true);
  $cats = explode('|',$cats);                       
  perch_blog_custom(array(
    'category' => $cats,
  ));
?>

With above, can get $cats to contain an array of relevant category handles. However when passed into the perch_blog_custom() function this returns empty.

Any thoughts?

Cole Henley

Cole Henley 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you print_r($cats) does it look like you'd expect?

Yep:

Array ( [0] => knowledge [1] => news )
Drew McLellan

Drew McLellan 2638 points
Perch Support

That looks like it should work. Are you categories in a set called blog ?

No, they're in a set called 'perspectives'. However, even if I update the page function no results are returned:

perch_blog_custom(array(
    'category' => $cats,
    'set' => 'perspectives',
));

Does perch_blog_custom support custom sets?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Blog expects things to use its category set unless you tell it otherwise. The category option takes category paths so use those.

Change:

<perch:category id="catSlug" type="slug" for="catTitle" /><perch:if exists="perch_item_last"><perch:else />|</perch:if>

to

perspectives/<perch:category id="catSlug" type="slug" for="catTitle" /><perch:if exists="perch_item_last"><perch:else />|</perch:if>

Brilliant thanks, hadn't appreciated the sets prefix.