Include blog on front page?

I’m trying to display the blog inside a div on my custom home page. I thought that just doing <?php include('blog.php') ?> on the template/home.php would do the trick. But it is throwing a Call to a member function page() on null error. If I get rid of the if($pagination->page() == 1):part of template/blog.php - I’ll just get a new error about Call to a member function count() on null - So it seems I’m approaching this in the wrong way…?

1 Like

You can get the blog page using the page helper_

<?php
$blogPage = page('blog');

You could then work it from there.

If you want to use part of your code from the blog template, I suggest you use a snippet inside your blog template that you can then reuse in the home template. It wouldn’t make sense to include the blog.php template, as that also contains a header/footer part that you would then duplicate.

Have. a look at the Starterkit for an example. The showcase snippet is used both in the home.php template and in the projects.php template.



1 Like

Sorry, I’m totally new to kirby and pretty dumb php wise. Could you elloborate on the page helper_ bit I don’t really understand? Where do I put that piece of code?

I’ve taken out the header and footer and unwanted bits of the blog template, so it could in theory just slide right into the main design.

//Edit, I should copy the (edited) blog.php to the snippets folder, and then call it on the home.php template with a <?php snippet('blog') ?>

Have a look at the example above.

Of course, you could just copy the relevant part from your blog template into the home template, but that way you would duplicate code.

In your blog template, you are probably fetching the subfolders like this:

$posts = $page->children()->visible();

This will not work on the home page, because $page refers to the current page. So to get the same page in your home template, you would have to use the page helper:

$posts = page('blog')->children()->visible();

Yes, you could do that. But keep in mind that you have to pass the page object to the snippet.

In your blog.php template, you would then call the snippet like this:

<?php snippet('blog') ?>

In your home.php template, like. this:

<?php snippet('blog', ['page' => page('blog')]) ?>
2 Likes