Archiv for Blog

Hi,
how can I make an archiv page(s) for my blog?

What I’ve done so far is

  • blueprint copy from „blog“ named „archiv“
  • template copy from „blog“ named „archiv“
  • folder in content named „archiv“ with „archive.txt“

In the panel, I get a new page called „Archiv“ where it lets me add page and files, but anyhow.

If I „click view“ page I get an error.

I am stuck, no idea what do do next.

PS: Should I mention, that I am a total newbie?

First of all, I need to know what sort of error you get? Secondly, I don’t have a crystal ball that lets me read your code from where I am :wink:, so I have no idea what could be going wrong…

Also, what are your plans with the archive page? Do you want to move pages there from the blog once they are outdated? (You can’t move pages in the Panel).

Ok, take this one for now ;):

The error says "This page is currently offline due to an unexpected error. "

Yes, I want to move post there, once the blog page has more than lets say 5-10 posts on it.

I really have no idea where to start.

Thanks, but it doesn’t work, I still can’t see anything.

Anyway, first of all, turn on debugging in your config.php.

c::set('debug', true);

Once you’ve done that, there should be a more telling error message. If you still can’t find your error, then maybe you want to make up your mind and post your code.

How do you write your blog posts? In the Panel?

The problem with moving stuff around is that the URL for your articles will change if you move them into another folder. So maybe this is not such a great idea? Why do you want to move post anyway? 5 to 10 posts in a folder is not really a lot. If you don’t want to show old posts, you might as well filter them out instead of moving them around.

And one more thing: make sure that your text file, the blueprint and the template have the same name, either archiv or archive, otherwise Kirby will fall back to the default template.

Coool!

The error is said to be in <?php if($articles->count()): ?> in my template (see downstairs)

Yes, I write my posts in the panel. And you are probably right - filtering old ones out is better than moving. But where do I filter them to? Still a different page, right?

And you were right - there was a typo in the “achiv(e)” YAML file. Here is it:


Title: Archiv

----

Text: 

----

Perpage: 500

This is the code of the template “archiv”:

<?php snippet('header-frontpage') ?>

    <main class="background-frontpage">
      <?php if($articles->count()): ?>
        <?php foreach($articles as $article): ?>

        <section class="blog-content">

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

            <p class="date"><?= $article->date('d.m.Y') ?></p>

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

            <div class="blog-text">
              <p class="excerpt"><?= $article->teaser() ?></p>
            </div>
              
             <div class="article-more-box">
                <a href="<?= $article->url() ?>" class="article-more">mehr..></a>
            </div>
        </section>

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

<?php snippet('my-footer') ?>
 </main>

Yes, you can have an archive page. In the template of the archive page, you would then filter all articles by date older than whatever.

I can only guess what is wrong with $articles->count() without the actual error message about which you are still very secretive, but you probably haven’t defined $articles anywhere?

You mean I shall post this?

``
Whoops\Exception\ErrorException thrown with message “Undefined variable: articles”

``

Oh, yes, I guess thats what it is saying up there.

The code of the archiv page/ template is a copy of the code in the template „blog“.

The Blog works, it fetches all blogposts and displays them. So the $articles must be definded somewhere - no? I thought, I would basically get the same output like the blog page and could then see how move on from there. Like how one can display just older post on the archive page.

Somehow like filtering methods

Edit:
Well the…hrm…controller file was …äh…missing. Didn’t know that I, äh, need one.

But the page doesn’t list any posts.

If you have just copied the blog controller, $articles will not return anything because $page in this case is the archive page which does not have any subpages.

You would have to replace $page->children() etc. with page('blog')->children() etc. and take it from there. After that you can filter by date. There’s a nice little Cookbook recipe all about filtering that should hopefully answer all your questions (if not, I promise I will update it).

Are controllers necessary: no, but they are are good way to keep the logic separate from the HTML.

No still an error:

ParseError thrown with message "syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'"

This is the controler:

return function($site, $pages, $page) {
 
  $perpage  = $page->perpage()->int();
  $articles = $page->('blog')->children()
                   	  ->visible()
                      ->flip()
                      ->paginate(($perpage >= 1)? $perpage : 5);
 
  return [
    'articles'   => $articles,
    'pagination' => $articles->pagination()
  ];
 
};

I must have still something done wrong.

$articles = page->('blog')->children()

Doesn’t work either.

Man, I gotta learn some PHP (sigh)

Both not quite correct, I’m afraid, correct would be:

 $articles = page('blog')->children();

Reading the docs for a start could help as well, just saying… :wink: (I probably have to slow down response times…)

Ahhhh yes!

No-on-no!

Thank you!!

Just to complete this:
Adding this one line at the right place to the stuff that was already in the controller took me hours of fun (kind of). And it works!

  ->filterBy('date', '<=', strtotime('2017-02-24'))
1 Like

Great! You are a true fighter! Now you only have to find a way to make this more dynamic, i.e.filter all articles that are older than 2 weeks, for example…

I tried. Maybe every possible combination from the examples of the docs and some googling for help, but - not having luck. Doing it wrong every single time. Speaking of it,

time())

is what I want to use, `cause it means “now”, right?

What I want to say is “filter by date using current date whatever “now” is at that moment, substract 1 year”. I read that there is “now” in php as well, but it doesn’t work either, so the best wrong code that I can come up with is:

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

  $perpage  = $page->perpage()->int();
  $articles = page ('blog')->children()
                   	  ->visible()
                      ->flip()
                      ->filterBy('date', '-1 year', time())
                      ->paginate(($perpage >= 1)? $perpage : 5);
  return [
    'articles'   => $articles,
    'pagination' => $articles->pagination()

  ];

};

This doesn’t show me an error message, but it doesn’t do anything.

Exactly, time() is now. And the strtotime() function is your friend in this case.

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

  $perpage  = $page->perpage()->int();
  $articles = page ('blog')->children()
                   	  ->visible()
                      ->flip()
                      ->filterBy('date', '<', strtotime("-1 year"))
                      ->paginate(($perpage >= 1)? $perpage : 5);
  return [
    'articles'   => $articles,
    'pagination' => $articles->pagination()

  ];

};

Oooooh. Of course.

Thanx!!!