gridnik
September 27, 2016, 2:21pm
1
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!
texnixe
September 27, 2016, 2:23pm
2
You could try this:
c::set('home', page('blog')->children()->sortBy('date')->first());
gridnik
September 27, 2016, 2:46pm
3
Thanks! How about if I want to do the same thing for project pages?
Projects
– 01-Project
– 02-Project
– 03-Project
About
Contact
texnixe
September 27, 2016, 2:50pm
4
What do you mean? Get the latest of both projects and blog?
gridnik
September 27, 2016, 2:55pm
5
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());
texnixe
September 27, 2016, 3:00pm
6
Ok, I made a stupid mistake, it should of course be children:
c::set('home', page('projects')->children()->visible()->last()->uid());
```
gridnik
September 27, 2016, 3:05pm
7
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
texnixe
September 27, 2016, 3:44pm
8
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 …
gridnik
September 27, 2016, 5:21pm
9
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?
texnixe
September 27, 2016, 5:40pm
10
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.
texnixe
September 27, 2016, 5:47pm
12
I don’t get the error in my test installation though, with the first solution. So I wonder why that happens.
gridnik
September 27, 2016, 5:50pm
13
Yes! Thanks @lukasbestle it works.
texnixe
September 27, 2016, 5:51pm
14
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.
But I’d use HTTP 302 anyway as the latest project will change over time. Otherwise the redirect gets cached.
gridnik
September 27, 2016, 6:09pm
16
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.
Perry
September 27, 2016, 9:08pm
17
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;
?>
texnixe
September 27, 2016, 10:25pm
18
Oh, good to know, I must have missed that