Startpage with one post per category
-
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
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
- 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
}
}
}
?>
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.
