We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Runway

Grabbing Current Categories in From Collection Item to Display Related Content

I'm trying to display content placed in the same category (set is called "tag") inside a detail page for a collection item. I'm putting this together from other forum topics. My initial fetch of the current categories outputting and I'm not sure what I'm missing. " '_id'=>$tagID," doesn't seem to work. Is there a correct way of fetching an array of existing categories assigned to a collection item?

if (isset($post[0]['tag']) && is_array($post[0]['tag'])) {
    foreach($post[0]['tag'] as $tagID) {
        $tags = perch_categories(array(
          '_id'=>$tagID,
          'skip-template' => true,
        ));
    }
}

  //Loop through tags and display related content
  foreach($tags as $tag) {
    perch_collection('Case Study', array(
      'template'        => 'case_studies/_case_studies_related.html',
      'category'        => $tag['catPath'],
      'category-match'  => 'any',
      'count'           => 3,
    ));
  }
Dan Lee

Dan Lee 1 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's the value of $tagID?

Dan Lee

Dan Lee 1 points

Looks like it is "tag/algae/"

There should be "tag/closed-loop/" assigned to the collection item too - so I'm guessing I need to make $tagID an array and then get that into the categories function? How do I do that?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not an ID, so you can't use it with the _id option. You'd need to use a filter instead.

Dan Lee

Dan Lee 1 points

Ok that _id was misleading. To clarify I want the path of all category items in the category set "tag" that have been assigned to a collection item.

I ultimatly want to place these paths into the category option so that it filters on the current categories, here are the paths hard coded:

'category' => array('tag/algae', 'tag/closed-loop'),

I'm struggling to know how to loop through and fetch the current category item paths. The following code echoes out only one category item path -

if (isset($test[0]['tag']) && is_array($test[0]['tag'])) {
  foreach($test[0]['tag'] as $tagName) {
    echo $tagName;
  }
}

In summary:

  1. I need to fetch all category item paths (not just the first one)
  2. Then assign them to a variable I can use in the above code.
Duncan Revell

Duncan Revell 78 points
Registered Developer

It looks like you've already got an array of category paths - $post[0]['tag'] - what's the value of that?

If it's an array of categories, use that in the 'category' filter.

Dan Lee

Dan Lee 1 points

Hi Duncan,

That only outputs one of the category paths "tag/algae/" and there is more that one that I need to output.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Ah, OK, I do something really similar with a collection and categories - for me, the $post[0]['tag'] holds both categories that have been assigned to a post (or news item in my case).

If print_r($post[0]['tag']) is only showing one entry in the array, do you need to investigate elsewhere for the cause of the issue?

Dan Lee

Dan Lee 1 points

That actually prints out the following: Array ( [0] => tag/algae/ [1] => tag/closed-loop/ ) which is good news

So to get this to work I just need to do the following? 'category' => $post[0]['tag'],

Duncan Revell

Duncan Revell 78 points
Registered Developer

Give it a go - but yes, it should work...

Dan Lee

Dan Lee 1 points

Here is the full solution for anyone else...

<?php

  $post = perch_collection('Case Study', [
        'filter'        => 'titleSlug',
        'match'         => 'eq',
        'value'         => perch_get('s'),
        'skip-template' => 'true',
        'return-html'   => 'true',
      ]);


//Display related Case Studies

//show results of tag array print_r($post[0]['tag']);

  perch_collection('Case Study', array(
    'template'              => 'case_studies/_case_studies_related.html',
    'category'              => $post[0]['tag'],
    'category-match'  => 'any',
    'count'                  => 3,
    // Filter Out Current Case Study
    'filter'                    => 'titleSlug',
    'match'                 => 'neq',
    'value'                  => perch_get('s'),
  ));

?>