Newsarticle or Video in Blog


I want a blog site with a news feed, where the entries are sorted by date. The articles should be either a normal article with text and an image etc. OR a vimeo video + title. So the feed should look like this:

Article 16. jul. 2018
Video 13. jul. 2018
Article 12. jul. 2018
Video 01. jun. 2018
Video 01. may 2018

I want to handle the articles via the children of the blog page. I’ve tried many things now, for example

    <?php if($page->template() == 'article'): ?>
      <?php foreach($data->children()->visible()->sortBy('date', 'desc') as $article): ?>
      code for article
      <?php endforeach ?>
    <?php else: ?>
      <?php foreach($data->children()->visible()->sortBy('date', 'desc') as $article): ?>
     code for videos
      <?php endforeach ?>
    <?php endif ?>

This just shows me the videos and no articles. What is the best solution for that?

I think I’m missing something here. What is $data?

Also, why do you separate by template if you want to show all articles in the loop sorted by date, no matter if they are video or article templates?

So we are on the blog template and want to fetch all the children, right?

My suggestion:

// lets grab all the babies
$articles = $page->children()->visible()->sortBy('date', 'desc');

// lets loop through the list
foreach($articles as $article) {
  // and call the snippet that corresponds to the intended template name
  snippet($article->intendedTemplate(), ['page' => $article]);

For this to work, you would have to create two snippets, article.php and video.php. Each snippet would contain the code for the individual article type.

On a side note: Please wrap code blocks within three backticks on a separate line before and after the block. Greatly enhances readability. Thanks you.

1 Like

I forgot to say. It’s a onepager thats why it says $data

Then you probably have to adapt this line to say:

$articles = $data->children()->visible()->sortBy('date', 'desc');

It really makes it easier if we have all the context right from the beginning… just saying.

1 Like

I don’t feel up to the mark right now, please bare with me.
I created the 2 snippets and pasted the code you suggested into the blog.php. But I’ve no idea how to finalize this right now.

How do I finish the foreach and do I have to put something else in the snippets but the raw snippet?

No problem! Please provide the code you got now in your template. I don’t know what you have before and after what you have posted above.


// lets grab all the babies
$articles = $data->children()->visible()->sortBy('date', 'desc');

// lets loop through the list
foreach($articles as $article) {
  // and call the snippet that corresponds to the intended template name
  snippet($article->intendedTemplate(), ['page' => $article])



<div class="uk-container">
 <div class="uk-flex uk-flex-center">
  <h4 class="uk-comment-title uk-margin-small-right"><?= $article->herausgeber()->html() ?></h4>
  <p class="article-date">|</p>
  <p class="article-date uk-margin-small-left"><?= $article->date('d.m.Y')?></p>
<article class="uk-comment">
<header class="uk-comment-header uk-grid-medium uk-flex-center" uk-grid>
  <div class="uk-width-expand">
    <ul class="uk-comment-meta uk-subnav uk-subnav-divider uk-margin-remove-top uk-flex uk-flex-center">
      <li class="pressetitel"><?= $article->title()->html() ?></li>
      <li><a href="<?php echo $article->presseBild()->toFile()->url() ? 
>">Zum Artikel</a></li>
<div class="uk-comment-body responsive_article">
    <iframe src="<?php echo $article->presseBild()->toFile()->url() ? 
 >&embedded=true" width="900" height="400"></iframe>


<p class="pressetitel"><?= $article->titel()->html() ?></p>
<?php echo vimeo($article->vimeoid()) ?>

And what I want is, that the customer decides if he wants to post a video or an article in the blog. I thought it’S the easiest by letting him chose the right blueprint. It can be by a radio button, I tried something with a radio button before, but it didn’t work.

You should use the variable $page for the article in the snippet, as that is what we are passing down to the snippet…

Why do you need an additional radio button if the user can already choose either the article or the video template/blueprint?

Not additional. Before I tried it with the blueprints I tried something else with radio buttons, because it would be the easiest in the panel I think if the user would choose in the panel if he wants to post an article or a video.

If the user only uploads either one video or one image and you don’t need additional fields, then you can do it with a radio button as well.

Otherwise, you either end up with fields that are not used or you would have to use a plugin that allows conditional fields.

The user also gives the article a title, which should be displayed above the pdf. The article itselfs is a pdf. Also the date should be written above. So it’s multiple fields.
For a video there is only the vimeo link, the title and the date.
Additional to all of that there should be an archive, but thats handled pretty easy by a checkbox and then the bool operator and so on…

I changed the variables. I get an error saying ‘syntax error, unexpected ‘}’’ on the foreach

The semicolon after snippet()… is missing

  snippet($article->intendedTemplate(), ['page' => $article]);
1 Like

Nice! It works! Thank you pretty much. One more question:
Inside the snippets I will put

 <?php if($page->archiv() == '0'): ?>

But how will I handle the ones where archiv is 1? Or is there a better way to create an archive?

Well I think it’s smarter to do a blogarchive where I copy the blog.php but put the if statement in there. I will try it out… Thank you pretty much again. This support is remarkable!

You can check if the value is true like this:

<?php if($page->archiv()->bool()): ?> 
  <!-- some HTML -->
<?php endif ?>

Or not bool, but what is this supposed to do? What do you want in the archive and what not? Do you really want to send articles to the archive manually (as opposed to by date)?

Or to rephrase: What is the purpose of the archiv field?

The purpose is that the one-pager isn’t flooded by articles but they should appear still somewhere. In the archive should be old articles which just would flood the default blog section. So below some articles will be a button to a different site. And I would call this site archive.

A solution where older articles than date X are automatically moved into an archive would be great too, but I don’t know how to realize this.

Well, what you could do is limit the number of articles that are displayed on the start/blog page:

$articles = $data->children()->sortBy('date', 'desc')->limit(5);

You can even make this limit dynamic with a field in the blog page where the user can set a limit. Then there is no need to manually select those blog pages.

Then in the archive, you use the same number as an offset instead of as limit:

$archive = $data->children()->sortBy('date', 'desc')->offset(5);

This will then fetch all but the first 5 articles

1 Like

This sounds great :+1:
Thank you so much again, I feel like I have to buy some more licenses to meet a bill :smiley:
I will do this anyways and I’m really looking forward to what future versions of Kirby are capable of

You will love Kirby 3!

1 Like