Forum
Processing Categories from perch_page_attributes.
As Simon stated at the end of this thread (forum.grabaperch.com/forum/01-20-2017-categories-in-page-attributes?page=1#reply-48876), the catPath isn't returned when using skip-template to return page attributes. So I'm trying to get the category information by using a template as described by Mark Melling in that same thread.
I am able to then process the category correctly and have the correct blog posts returned when there's only one category specified. However, I can't figure out how to get it to work when there's more than one category specified.
Here's my code:
<?php
$blog_cats = perch_page_attributes(['template' => '_show_blog' ],true);
$blog_cats = trim($blog_cats);
echo($blog_cats);
perch_blog_custom([
'template' => 'story_jump_listing_carousel',
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'sort-type' => 'alpha',
'category' => $blog_cats,
'category-match'=> 'any' ]);
?>
the page attributes template _show_blog
is set to output the categories like this:
if one category is specified, the template returns:
blog/for-love-of-the-coast
for more than one category, the template returns:
['blog/for-love-of-the-coast', 'blog/people-make-it-possible']
So those are the values for $blog_cats
.
It works for one category, not for more than one.
I've tried several different ways to try and pass in the multiple values but I can't figure out what would work. Any help would be much appreciated.
You can use the PHP
explode()
function to break a string into an array of strings.Have you tried hardcoding it first? The paths may need a trailing slash
blog/for-love-of-the-coast/
Thank you both! Exploding the result is the solution. Awesome. And Hussein, in this same project I needed to add trailing slashes elsewhere when building the category path from a collection item, but in this case, it works both with and without the trailing slash. I think perch handles categories a little differently when called from the
perch_blog_custom
.Thanks again!
I'll put the final code here in case it helps someone (and for when I forget the solution). It only works if you clean the result from the template with a
trim
as shown below:on the page template:
and the page attributes template that creates the category strings, in my case
_show_blog.html
:output from the
_show_blog
template looks like this:For one category:
For more than one: