Help with 'dynamically inserting' categories into pagination

Hi: I am trying to figure out how to ‘dynamically insert’ (if that is the right term!) a category name into the pagination code below:
Currently I am hard coding ‘home’ so that all pages with this category go from previous to next - and all works well - and other listed pages are not included in the pagination.

However I was wondering if there was a way to replace ‘home’ with the current category of the listed pages so I wouldn’t have to create separate templates, etc.

<?php $collection = $page->siblings()->listed()->filterBy('categories', 'home', ','); ?>

Thanks so much for any help!

Hm, I don’t quite understand what this has to do with pagination and where the dynamic category would come from?

Sorry, let me explain a bit! An artwork portfolio site where I have categories created which can be checked off in the panel - that allow certain artworks to show up. So let’s say five artworks have the ‘home’ checkbox checked, and another five have ‘private view’ checked.

The following code grabs all the pages in the ‘home’ category.

<?php foreach ($privatePages = $pages->children()->filterby('categories', 'home', ',') as $artwork): ?>

And this is fine to have the category inserted manually.


This is the blueprint:

categories:
                label: Visibility Section
                type: checkboxes
                default: archive
                options:
                  home: home
                  privateview: private view
                  preview: preview
                  studies: studies
                  review: review
                  exhibition: exhibition
                  archive: archive

So on the front end - when someone clicks on the artwork link to the ‘detail page’, I want the ‘NEXT | PREVIOUS’ links (on the detail pages) to only show the pages listed within a certain category. I have it working if I hard code the category ‘home’ (for example) in this template code:

<?php $collection = $page->siblings()->listed()->filterBy('categories', 'home', ','); ?>

This is then followed by:

<ul class="pagination">

  <?php if ($page->hasPrevListed($collection)) : ?>
    <li class="prev">
      <a href="<?= $page->prevListed($collection)->url() ?>">< PREVIOUS </a>
    </li>
  <?php endif ?>

  <?php if ($page->hasNextListed($collection)) : ?>
    <li class="next">
      <a href="<?= $page->nextListed($collection)->url() ?>">NEXT ></a>
    </li>
  <?php endif ?>

</ul>

What I was wondering is if I could replace ‘home’ with some code that grabs the current category from somewhere.

I hope that makes sense!

You can send the current category as a url parameter in your link to the detail page, then get this param via the param() helper.

I will need help with that I think! Can you show me how I would do that?

This is my current link to the detail page:

<a href="<?= $artwork->url() ?>">

Then where would I place param()…

Sorry to be a pest, but I am learning as I go along here!

<a href=" <?= url($artwork->id(), ['params' => ['category' => 'home']]); ?>">

Then in your destination template, get the param with:

$param = param('category'); // will return `home` in the example

Almost there I think!

Here is the url on the detail page:

localhost/artworks/blast/category;home

Unfortunately I am not understanding the next part:

$param = param('category');

Where should I place this? I have tried (believe me) but keep getting errors, or no Previous next link showing.

Here is the code again on the detail page:

<?php $collection = $page->siblings()->listed()->filterBy('categories', 'home', ','); ?>

<ul class="pagination">

  <?php if ($page->hasPrevListed($collection)) : ?>
    <li class="prev">
      <a href="<?= $page->prevListed($collection)->url() ?>">< PREVIOUS </a>
    </li>
  <?php endif ?>

  <?php if ($page->hasNextListed($collection)) : ?>
    <li class="next">
      <a href="<?= $page->nextListed($collection)->url() ?>">NEXT ></a>
    </li>
  <?php endif ?>

</ul>

Thank you!

On the page where you land after clicking the link with the parameter

<?php 
$param = param('category');
$collection = $page->siblings()->listed()->filterBy('categories', $param ','); ?>

Unfortunately this gives an error
(I can’t imagine trying to debug this from your side, so this help is very much appreciated!)

<?php 
$param = param('category');
$collection = $page->siblings()->listed()->filterBy('categories', $param ','); ?>



<ul class="pagination">

  <?php if ($page->hasPrevListed($collection)) : ?>
    <li class="prev">
      <a href="<?= $page->prevListed($collection)->url() ?>">< PREVIOUS </a>
    </li>
  <?php endif ?>

  <?php if ($page->hasNextListed($collection)) : ?>
    <li class="next">
      <a href="<?= $page->nextListed($collection)->url() ?>">NEXT ></a>
    </li>
  <?php endif ?>

</ul>

OK, i put a comma after param - didn’t get an error
However, I only got the ‘NEXT’ LINK to appear on the page - and then no links at all after clicking on next.
Very close though!

<?php 
$param = param('category');
$collection = $page->siblings()->listed()->filterBy('categories', $param, ','); ?>

Sorry, yes, I forgot the comma.

Because the prev and next links will then need the parameter as well

1 Like

This is giving me an error, but am I close?? (!)

<a href="<?= $page->prevListed($collection)->url() $param ?>">< PREVIOUS </a>

Well, that syntax is not correct, you can pass the parameter to the url function like before:

<?= $page->prevlisted($collection)->url(['params' => ['category' => $param]]); ?>

Wonderful! A big thank you to both of you!
All working great.