Forum

Thread tagged as: Question, Runway

Paginate categories inside collection

Is it possible to paginate categories that are stored inside a collection item?

For example:

  • I have a collection called Flooring Industries, which could be made up of things like Hotels, Retail, Education, etc.
  • Each Flooring Industry collection has flooring 'categories' attached to it e.g. Retail might have flooring line categories such as Lines, Stripes, Squares, etc.

I need to show each industry, and its associated categories.

On an example page, Andover and Fernlea are flooring lines (Perch categories) attached to the Serviced Offices collection. I would like to paginate the categories here.

I initially thought that if I paginate the collection this would work, but instead, Perch paginates the collection item. e.g. something like the following would paginate Hotels, Retail, Education, rather than Lines, Stripes, Squares:

perch_collection('Flooring Catalogue Industries', [
    'filter'        => 'name_link',
    'match'         => 'eq',
    'value'         => perch_get('level3'),
    'template'      => 'flooring_catalogue/industries/industry_categories.html',
    'paginate'      => true,
    'count'         => 1,
]);

So what I need to do is paginate the categories inside this collection.

My guess is that:

  1. I might have missed something obvious and this is easily achieved, or…
  2. I might need to do something with a callback

Any suggestions appreciated!

Jay George

Jay George 2 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Jay,

I initially thought that if I paginate the collection this would work, but instead, Perch paginates the collection item

perch_collection() gets you Collection items. perch_categories() gets you Categories. So if you need to paginate Categories, you ultimately need to use perch_categories().

What you can do in this case is retrieve the Collection items with perch_collection() and use the skip-template option. Grab all the used categories in PHP, then use them to filter the categories with perch_categories().

$items = perch_collection('Flooring Catalogue Industries', [
'skip-template' => true,
]);

// grab the categories from $items

// use perch_categories() to output the categories (paginated and filtered)

Thanks, Hussein! That makes sense.

I'm currently stuck on how to grab just the categories from the array. So I have this:

$items = perch_collection (
    'Flooring Catalogue Industries', [
        'skip-template' => true,
        'filter'        => 'slug',
        'match'         => 'eq',
        'value'         => perch_get('level3'),
    ]
);

…which gets me the correct collection record for the page.

How would I drill down further, just to grab the categories from that collection?

Thanks again for your help

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

You can check what you have with:

print '<pre>';
print_r($items);
print '</pre>';

Something roughly like this should do the trick:

$categories = array();

// Loop through $items
foreach($items as $item) {
    // loop through categories if exist for $item
    if(isset($item['categories'])) {
        foreach($item['categories'] as $cat) {
            $categories[] = $cat;
        }
    }
}


// remove duplicates
$categories = array_unique($categories);


// display categories
perch_categories([
    'filter' => 'catPath', // or catID depending on what you get in $items
    'match' => 'in',
    'value' => implode(',', $categories),
    'paginate' => true
]);

Here $item['categories'] refers to $item['field_id']. So assuming your categories field has the id categories:

<perch:categories id="categories" label="Categories" set="flooring" />

Word processing, desktop publishing, and digital typesetting are technologies built on the idea of print as the intended final output medium, although nowadays it is understood that plenty of the content produced through these pathways will be viewed onscreen as electronic pages by most users rather than being printed on paper. https://mcdvoice.me/

How to use https://iptvlinks.site/ for 2019