Set homepage as most recent content post

I have a content folder with numerous dated articles. I would like to somehow route the default homepage to simply show the most recent article using the article template (building off the ‘Get Started with Kirby’ starter site). In other words, when you go to example.com, it’ll just autoroute to example.com/article-name.

I tried changing the home config to c::set('home', $pages('home')->children()->last()); as per https://getkirby.com/docs/cheatsheet/options/home and https://forum.getkirby.com/t/changing-home-page-in-kirby-demo-template/4973 but then the site wouldn’t load at all (no errors, even with debug turned on).

I also tried forcing the home template to grab the most recent article but the prevnext snippet doesn’t work right and it seems redundant to duplicate that code.

Should be:

c::set('home', page('home')->children()->last());
1 Like

Ahhhhhh thanks that did it!

Follow up question — is there a way to make the url update accordingly? i.e. I enter website.com but the site automatically redirects to website.com/articlename

Hm, that’s strange. If you set home to a subpage, it should return the subpage. I can’t really reproduce your redirect. Are you using any routes that might interfere, maybe?

Just to clarify, the current behavior is I enter website.com and get website.com with the last article displayed. I would like the site to actually display that article’s url, i.e. website.com/articlename.

Not using any routes that I am aware of…

Oh, I see.

Hm, not sure how to achieve this without running into an endless loop. Maybe instead of setting the child page as the homepage redirect the home page to the subpage?

How would I set that up dynamically, if the most recent article changes? I guess it would be ok if the first article was just website.com but it seems to clunk up the javascript ajax navigation stuff I’m doing.

I’m essentially trying to set up an endless loop of articles like this: http://barbajs.org/demo/nextprev/index.html.

Maybe a better question is why the else statements in this pagination snippet return website.com instead of website.com/first-article. Going off of this thread: Navigation / pagination loop

<?php
if($page->hasNextVisible() || $page->hasPrevVisible()): ?>
  <nav class="pagination wrap cf">

    <?php if($page->hasPrevVisible()): ?>
      <a class="pagination-item nav prev" href="<?= $page->prevVisible()->url() ?>" rel="prev" title="<?= $page->prevVisible()->title()->html() ?>">&rsaquo;</a>
    <?php else: ?>
      <a class="pagination-item nav prev" href="<?= $page->last()->visible()->url() ?>" rel="prev">&rsaquo;</a>
    <?php endif ?>

    <?php if($page->hasNextVisible()): ?>
      <a class="pagination-item nav next" href="<?= $page->nextVisible()->url() ?>" rel="next" title="<?= $page->nextVisible()->title()->html() ?>">&lsaquo;</a>
    <?php else: ?>
      <a class="pagination-item nav next" href="<?= $page->first()->visible()->url() ?>" rel="next">&lsaquo;</a>
    <?php endif ?>

  </nav>
<?php endif ?>

$page->last() and $page->first() doesn’t make sense. These methods are only valid in collections… and a single $page object is not a Collection/Pages object.

But this whole thing is intended to be used between siblings, the homepage, however, is not part of the siblings collection.

Tonight I’m too tired to think about this… :sleepy:

All good! I was able to make it work.

<?php 
  $subpages = page('home')->children()->visible();
  if($page->hasNextVisible() || $page->hasPrevVisible()): ?>
  <nav class="pagination wrap cf">

    <?php if($page->hasPrevVisible()): ?>
      <a class="pagination-item nav prev" href="<?= $page->prevVisible()->uid() ?>" rel="prev">&rsaquo;</a>
    <?php else: ?>
      <a class="pagination-item nav prev" href="<?= $subpages->last()->uid() ?>" rel="prev">&rsaquo;</a>
    <?php endif ?>

    <?php if($page->hasNextVisible()): ?>
      <a class="pagination-item nav next" href="<?= $page->nextVisible()->uid() ?>" rel="next">&lsaquo;</a>
    <?php else: ?>
      <a class="pagination-item nav next" href="<?= $subpages->first()->uid() ?>" rel="next">&lsaquo;</a>
    <?php endif ?>

  </nav>
<?php endif ?>

Then I used this plugin from this thread to get the homepage to redirect to the first article.

I would just have added a redirect in the home template instead of using a plugin, but that’s up to you. Maybe you need it for other purposes as well.