Startpage with one post per category

Post Reply
kr428
Sr. Bludit
Posts: 45
Joined: Thu Jan 05, 2023 9:07 pm
Has thanked: 14 times
Been thanked: 4 times

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
User avatar
Edi
Site Admin
Posts: 3115
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 76 times
Been thanked: 116 times
Contact:

You have to code it.

Perhaps I have some time this or next week to try it.
clickwork.ch, digitale Projekte
Bludit-Tipps, Erklärungen und Anleitungen
User avatar
SpegalDev
Sr. Bludit
Posts: 38
Joined: Tue Jun 13, 2023 11:27 pm
Location: Michigan, USA
Has thanked: 6 times
Been thanked: 34 times
Contact:

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
        }
    }
}
?>
Image BluditLab — Explore a comprehensive collection of Bludit themes and plugins, offering both free and premium options to elevate your Bludit site. Find the perfect tools for customization, SEO, and more.
Post Reply