Forum
Perch Blog — related posts from same category
Hi there,
I've followed a series of posts from the forums on getting related posts working.
I have a set called 'news' for which there are two categories: Athlete Spotlight
and Articles
. If I run the following I get some errors.
//Get the categories for the current post
$categories = perch_blog_post_categories(perch_get('s'), array(
'skip-template'=>true, ));
print_r($categories);
//The post may be in more than one category, so we need to loop through these and get the category slugs
if (count($categories)) {
$cat_slugs = array();
foreach ($categories as $cat) {
$cat_slugs[] = $cat['catSlug'];
print_r($cat);
}
}
The printed output it gives me is:
Array ( [0] => Array ( [desc] => Array ( [_flang] => markdown [raw] => Latest News [processed] =>
Latest News
) [catID] => 1 [setID] => 4 [catParentID] => 0 [catTitle] => Articles [catSlug] => articles [catPath] => news/articles/ [catDisplayPath] => Articles [catOrder] => 1 [catTreePosition] => 004-001 [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"Latest News","processed":"
Latest News<\/p>"}} [perch_desc] => Array ( [_flang] => markdown [raw] => Latest News [processed] =>
Latest News
) [catDepth] => 1 ) ) Array ( [_flang] => markdown [raw] => Latest News [processed] =>
Latest News
) 140Articlesarticlesnews/articles/Articles1004-001{"desc":{"_flang":"markdown","raw":"Latest News","processed":"
Latest News<\/p>"}}Array ( [_flang] => markdown [raw] => Latest News [processed] =>
Latest News
) 1
and in the perch debug I get this error:
Array
(
[type] => 8
[message] => Undefined index: catSlug
[file] => /var/www/sitename/news/post.php
[line] => 49
)
But the catSlug
is the right reference to that information so I'm confused as how to its undefined. The posts are definiately all assigned to either articles or athlete spotlight.
Can you edit each of your categories in turn? Just click save on them.
Hi Drew,
Just done that, although still appear to be getting the same error. I've also just tried deleting all my posts and re-adding some new ones to each category
Is that code above verbatim, or have you edited stuff out?
The array
print_r
are exactly as they come out.My post page in it's entirety looks like:
I can't marry up what's happening between the first
print_r
and the second. What you have there should be correct as far as I can see.Yeh me too. I will keep working away. Like you said though I've been through this a million time and everything looks fine - like it should work.
The only way I managed to get
Is to change
to
and I then see two stories from either category. Switch is back to the previous and I see nothing being output again. But causes the error:
Should I able to to see
catSlug
in the showall output for the post?You need to return an array (with skip-template) in order to be able to iterate over them.
When you loop through you should have each category, but somehow you end up with the description field.
Ill try make a fresh project this weekend in order to test this on a project where everything is set back to default and try track down what's happening from there. It must be something I've done within the project files for the blog that is causing this.
Just quick one - would using a set of categories under a set called 'news' rathern than 'blog' be a problem?
No, that's fine. If you want to use the blog functionality around categories you'd need to use the dedicated set, but you can do whatever you want to with it.