Pagination with tag filtering

Hi all -

I’m trying to add pagination to my projects page which already uses tags.

I’ve tried using the tut on this page but no luck:

Can anyone help shed some light on how I could use this code while maintaining the tag filtering option?

My current code is

	<div class="row text-center taglist">
		<div class="col-lg-12">
	  <?php if($tag = param('tag')): ?>
	    <a href=<?php echo url('projects')?>>← All tags</a>
	  <?php endif ?>
	  <?php foreach($tags as $tag): ?>
	    <a class="<?php echo html($tag) ?>" href="<?php echo url('projects/' . url::paramsToString(['tag' => $tag])) ?>">
	      <?php echo html($tag) ?>
	    </a>
	  <?php endforeach ?>
	</div>
	</div>

	<div class="row">
	  <?php foreach($projects->limit(9) as $project): ?>
		  <a class="col-lg-4 col-md-6 col-sm-12 project-box flex" href="<?php echo $project->url() ?>"
			<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
			style="background-image: url('<?php echo $image->url() ?>'); background-size: cover; background-repeat: no-repeat;
			background-position: center; ">
		<?php endif ?>>
				<h3><?php echo $project->title()->html() ?></h3>
  		</a>
  		<?php endforeach ?>
	</div>

Thanks so much!

See this post: Pagination with tags

The important part here is that the pagination needs to be applied to the already filtered post/projects/whatever.

1 Like

Thanks - I forgot to show the link to the tut I’m following: https://getkirby.com/docs/cookbook/pagination

So where am I actually adding ‘->paginate(10)’ ?

Well, I don’t see any pagination happening in your code at all.

Guess you must have defined your projects somewhere (in your controller? At the top of the template?)

No I haven’t added the pagination as I’m clueless where to do it.

Yes I have defined in controller:

<?php
return function($site, $pages, $page) {

  // fetch the projects
  $projects = page('projects')->children()->visible();

  // fetch all tags used in projects. pluck($field, 'separator', unique)
  $tags = $projects->pluck('tags', ',', true);

  // add the tag filter if there is a tag in the url
	if($tag = param('tag')) {
	  	$projects = $projects->filterBy('tags', $tag, ',');
	}

	// Return the list of projects and tags to template
  	return compact('projects', 'tags');
};
?>
<?php
return function($site, $pages, $page) {

  // fetch the projects
  $projects = page('projects')->children()->visible();

  // fetch all tags used in projects. pluck($field, 'separator', unique)
  $tags = $projects->pluck('tags', ',', true);

  // add the tag filter if there is a tag in the url
	if($tag = param('tag')) {
	  	$projects = $projects->filterBy('tags', $tag, ',');
	}
  $projects = $projects->paginate(10);
  $pagination = $projects->pagination();
	// Return the list of projects and tags to template
  	return compact('projects', 'tags', 'tag', 'pagination);
};

(no closing php tag at end of controller)

Fantastic, all sorted. Thanks so much :slight_smile: