Blog template for scenario where only some articles are translated

Hi there! I am a bit lost.
What I like to achieve is sth I read a few times here but can’t find a concrete answer:

I have a multilingual page setup (en default, de). But only the blog section should behave differently. We will post some articles only in English, some only in German, and some will be translated.
So /blog should only list English posts, /de/blog should only list German posts.

I found @texnixe ’s solution for this, but it seems outdated. I downloaded the latest starter kit. Since I am a php newbie, I would love to hear suggestions from you. Thanks in advance. – Martin

I guess the magic happens here:


return function($site, $pages, $page) {

  $perpage  = $page->perpage()->int();
  $articles = $page->children()
               ->paginate(($perpage >= 1)? $perpage : 5);

  return [
'articles'   => $articles,
'pagination' => $articles->pagination()

blog template:

<header class="wrap">
  if($pagination->page() == 1):
  <?php endif ?>
  <hr />

<section class="wrap">
  <?php if($articles->count()): ?>
    <?php foreach($articles as $article): ?>

      <article class="article index">

        <header class="article-header">
          <h2 class="article-title">
            <a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a>

          <p class="article-date"><?= $article->date('F jS, Y') ?></p>

        <?php snippet('coverimage', $article) ?>

        <div class="text">
            <?= $article->text()->kirbytext()->excerpt(50, 'words') ?>
            <a href="<?= $article->url() ?>" class="article-more">read more</a>


      <hr />

    <?php endforeach ?>
  <?php else: ?>
    <p>This blog does not contain any articles yet.</p>
  <?php endif ?>

<?php snippet('pagination') ?> 

You can filter your page collection like this:

$articles = $page->children()->visible()->filter(function($child) {
  return $child->content(site()->language()->code())->exists();
1 Like

Yay, that instantly worked! Thanks a lot. Will test further.

Hi, again regarding this one.

Does it mean I only can create a German-only blog post (en is default lang) without the panel? Because when adding a German blog post via panel, a en text file is created in every case.

If this is the case, I wonder how I could filter blog post like “when “text” field is not empty”?

Thanks for your ideas! –Martin

You could check if the file exists and the field is empty:

$articles = $articles->filter(function($child) {
  return $child->content(site()->language()->code())->exists() && $child->content(site()->language()->code())->get('text')->isNotEmpty();

This might get a bit tricky with the pagination, I just had a strange behaviour in the langkit. Will check later. But you’ll probably have to use a custom page method to get that right.

I have downloaded the original Kirby Langkit version 2.4.0 and installed it on XAMPP with Windows.
Then I have changed the default language to German and changed the German ‘url’ to ‘/de’ in the file \site\config\config.php.
If I go to “/panel/pages/blog/content/edit”, I see this content file article.en.txt with the fields of the blueprint default.yml, because there is no content file It is unimportant whether in my profile DE or EN is selected as language. Also, the language whose fields appear in the panel is not relevant to this behavior.

I think this is an error in the actual panel version. What do you think @texnixe?

So I have to answer your question with: yes!

I’ll have to look into this. But I don’t quite see how it relates to @martinthiemann’s issue?

1 Like

To simulate his problem, I only changed DE and EN related to the quoted problem description from the previous post, so we can use the default download as I sayed in my post.

I discovered this yesterday…

@anon77445132: If you change the default language, you have to rename the text files if you want to use the panel. The panel always creates the textfile for the default language automatically.

Sorry @texnixe, I think that was not the question of @martinthiemann. [quote=“martinthiemann, post:4, topic:5882”]
Does it mean I only can create a German-only blog post (en is default lang) without the panel?

@anon77445132: As I said above, the panel always creates the textfile for the default language when you create a page in a non-default language. This was to confirm @martinthiemann’s question.

But that not really the important part here, the question is how to filter content to get only the content in a particular language, no matter in what language that content was created and to prevent the fallback to the default language.