Forum
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,
));
}
What's the value of
$tagID
?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?
That's not an ID, so you can't use it with the
_id
option. You'd need to use afilter
instead.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:
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 -
In summary:
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.Hi Duncan,
That only outputs one of the category paths "tag/algae/" and there is more that one that I need to output.
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?That actually prints out the following:
Array ( [0] => tag/algae/ [1] => tag/closed-loop/ )
which is good newsSo to get this to work I just need to do the following?
'category' => $post[0]['tag'],
Give it a go - but yes, it should work...
Here is the full solution for anyone else...