Page 1 of 1

Startpage with one post per category

Posted: Thu Aug 08, 2024 12:31 pm
by kr428
Hi alls;
in order to tweak my blog a bit, I wonder whether I can set up a start page which doesn't display much more than just the most current post in each category I use, plus the most recent post that has no category assigned... is that possible? Where to start if so?
Thanks very much,
Kristian

Re: Startpage with one post per category

Posted: Mon Aug 12, 2024 5:57 pm
by Edi
You have to code it.

Perhaps I have some time this or next week to try it.

Re: Startpage with one post per category

Posted: Sat Aug 17, 2024 1:21 am
by SpegalDev
I haven't tested the code, but give this a try:

Code: Select all

<?php
$categories = $site->categories();
$displayedPosts = [];
foreach ($categories as $categoryKey => $category) {
    $posts = $category->posts();
    if (!empty($posts)) {
        $firstPost = reset($posts);
        if (!in_array($firstPost->key(), $displayedPosts)) {
            $displayedPosts[] = $firstPost->key();
            ?>
            <article>
                <h2><?php echo $firstPost->title(); ?></h2>
                <p><?php echo $firstPost->content(); ?></p>
                <a href="<?php echo $firstPost->permalink(); ?>">Read More</a>
            </article>
            <?php
        }
    }
}
?>