Latest article as home page

Hello,

Here is my site structure:

  • Blog
    – 01-Article
    – 02-Article
    – 03-Article
  • About
  • Contact

I want to redirect my website home page to latest blog article. So the latest article is going to be my home page with the actual article URL.

I guess I can do that with route in config.php but not sure how?

Thanks!

You could try this:

c::set('home', page('blog')->children()->sortBy('date')->first());

Thanks! How about if I want to do the same thing for project pages?

  • Projects
    – 01-Project
    – 02-Project
    – 03-Project
  • About
  • Contact

What do you mean? Get the latest of both projects and blog?

Sorry, I mean the latest post of projects section.

I tried to edit your code but it didn’t worked:
c::set('home', page('projects')->project()->sortBy('date')->first()->uid());

Ok, I made a stupid mistake, it should of course be children:

c::set('home', page('projects')->children()->visible()->last()->uid());
```

Now I’m getting this error:

Fatal error: Uncaught Error: Call to a member function isErrorPage() on boolean in /Users/admin/Dropbox/Sites/mywebsite/kirby/kirby.php:333 Stack trace: #0 /Users/admin/Dropbox/Sites/mywebsite/kirby/toolkit/helpers.php(270): Kirby->{closure}(‘/’) #1 /Users/admin/Dropbox/Sites/mywebsite/kirby/kirby.php(678): call(Object(Closure), Array) #2 /Users/admin/Dropbox/Sites/mywebsite/index.php(16): Kirby->launch() #3 {main} thrown in /Users/admin/Dropbox/Sites/mywebsite/kirby/kirby.php on line 333

Maybe it is just not possible because the page object is not available when the config file is called.

As an alternative, you could redirect to the page you want in the home template.

Edit: I just tested this code:

c::set('home', kirby()->site()->find('projects')->children()->visible()->first());

seems to work …

Thanks. Yeah, it’s working but with an error:

Warning: Illegal offset type in /Users/admin/Dropbox/Sites/mywebsite/kirby/toolkit/lib/router.php on line 142

As an alternative, you could redirect to the page you want in the home template.

How can I do that?

You can use the header::redirect method: https://getkirby.com/docs/toolkit/api/header/redirect

Or just:

go(page('projects')->children()->visible()->last());

This should be inside your home.php template. The home option needs to be removed again of course.

I don’t get the error in my test installation though, with the first solution. So I wonder why that happens.

Yes! Thanks @lukasbestle it works.

The advantage of using header::redirect is that you can use pass a http code as an argument, which you can’t do with the go() function.

You can since Kirby 2.3.2. :slight_smile:

But I’d use HTTP 302 anyway as the latest project will change over time. Otherwise the redirect gets cached.

Thanks so much @texnixe but I couldn’t managed to make it work with header::redirect function. My PHP abilities are limited (limited as in none). So it’s not easy for me to use any function without a detailed explanation.

in your home template:

<?php 
$exclude = array('home');


foreach($site->index()->visible()->not($exclude)->sortBy('modified', 'desc')->limit(3) as $items):
{
	echo "<div class='grid-24-7'>";
	echo "<a href=".$items->url()."><h3>".$items->title()."</h3><img src=".thumb($items->images_news()->toFile(), array('width' => 600))->url()."></a>";
	echo "</div>";
	

}
endforeach;


?>

Oh, good to know, I must have missed that :slight_smile: