Forum

Thread tagged as: Question, Runway

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.

Monty Lewis

Monty Lewis 2 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use the PHP explode() function to break a string into an array of strings.

$cats_str =  'blog/for-love-of-the-coast|blog/people-make-it-possible';
$blog_cats = explode('|', $cats_str);
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Have you tried hardcoding it first? The paths may need a trailing slash blog/for-love-of-the-coast/

$blog_cats = ['blog/for-love-of-the-coast/', 'blog/people-make-it-possible/'];

perch_blog_custom([
'template' => 'story_jump_listing_carousel',
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'sort-type' => 'alpha',
'category' => $blog_cats,
'category-match'=> 'any' 
]); 

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:
    $cats_str = perch_page_attributes([
        'template' => '_show_blog'      
    ],true);

    $cats_str = trim($cats_str);

    $blog_cats = explode('|', $cats_str);

//  echo($cats_str); // for testing
  perch_blog_custom([
     'template'      => 'story_jump_listing_carousel',
     'sort'          => 'postDateTime',
     'sort-order'    => 'DESC',
     'sort-type'     => 'alpha',
     'category'      => $blog_cats,
     'category-match'=> 'any'  ]);  

and the page attributes template that creates the category strings, in my case _show_blog.html:

<perch:categories id="story_category" label="Post Category" set="blog" ><perch:if id="perch_item_count" value="1" match="gt"><perch:if exists="perch_item_first"></perch:if></perch:if>blog/<perch:category id="catSlug" type="text" />/<perch:if exists="perch_item_last"><perch:if id="perch_item_count" value="1" match="gt"></perch:if><perch:else />|</perch:if></perch:categories>
output from the _show_blog template looks like this:

For one category:

blog/forever-protected/

For more than one:

blog/forever-protected/|blog/caring-for-the-land/