Hi everyone, I’m trying a whole lot to add categories to what I’m working on. I did manage to have different filter my posts but so far I cant for dear life lists the category of each post.
This is what I’m working with:
controller
<?php
return function($page) {
// fetch the basic set of pages
$posts = $page->children()->listed()->flip();
// fetch all categories
$categories = $posts->pluck('categories', ',', true);
// add the category filter
if($category = param('category')) {
$posts = $posts->filterBy('categories', $category, ',');
}
// apply pagination
$posts = $posts->paginate(5);
$pagination = $posts->pagination();
return compact('posts', 'categories', 'category', 'pagination');
};
Where I’m trying to display tags
<section class="categories">
<a href="<?= $page->url() ?>">All</a>
<?php foreach($categories as $category): ?>
<a href="<?= url($page->url(), ['params' => ['category' => $category]]) ?>" <?php e($category === param('category'), 'class="active"', '') ?>><?= html($category) ?></a>
<?php endforeach ?>
</section>
<section>
<?php foreach($posts as $post): ?>
<article>
<a href="<?= $post->url() ?>"><h3><?= $post->title()->html() ?></h3></a>
<p>On <time><?= $post->date() ?></time>, listed under
<span class="category">
// this is where I'm trying to display categories
</span></p>
</article>
<?php endforeach ?>
</section>
<?php snippet('pagination', ['pagination' => $posts->pagination()]) ?>
Please believe me, I searched everywhere but I don’t understand what I must do to get this working.
Thank you