Blog

Showing posts tagged #web dev

Tags

Tried to upgrade to Kirby 3.7 but that seems to break the Commentions plugin, which I barely got working to begin with. I'm smart enough to build a website, but not smart enough to make it fancy 😬

Well would you look at that, I can officially tweet from my own website. "Officially" of course not meaning that it's auto-posted to Twitter, but tweet-like notes can now have a permanent home here.

Listing year or month only once in blog archive

A few variations of this website ago, I decided I really liked the idea of listing the posts in my blog arranged by year and month. Basically, instead of each entry having the full date (year, month, and day), I want the list of blog posts to look like this:

- 2022
    - May
        - Blog post
        - Blog post
        - Blog post
    - April
        - Blog post
    - March
        - Blog post
- 2021
    - December
        - Blog post
        - Blog post

Rather than creating loops for each year and month, I wanted to be able to just pop the year and month into separate paragraph or header tags, and only actually generate those when required — aka when it's the first time it's being listed, and no other time.

I think there might be a function in WordPress that does this automatically, but getting it to work in another CMS, including Kirby, resulted in some head-scratching at first. I'm writing this here for my future self and anyone else who wants it.

Basically, we need to take advantage of the foreach loop and some extra variables to make this work. For example, to only list the year the first time, we'll create a new variable $posted_year just before the loop, and set it to something like 0. Then, inside the loop, we'll compare that variable to the year of the current post (via another variable $post_year here) — if it does not match (eg. 0 ≠ 2022), then the year is shown. Then, still in the loop, we'll set that new variable to the year of the current post. When the function loops again, the variable is now set as the year of the previous post, so when we test if it matches the current post and it does (2022 == 2022), we just... don't post the year.

<?php
   $posted_year = 0;
   $posted_month = 0;
   foreach($page->children()->flip() as $blogpost):
      $post_year = $blogpost->date()->toDate('Y');
      $post_month = $blogpost->date()->toDate('mY');
      if($post_year != $posted_year):
         $posted_year = $post_year; ?>
         <h2><?= $posted_year ?></h2>
      <?php endif ?>
      <?php if($post_month != $posted_month):
         $posted_month = $post_month; ?>
         <h3><?= $blogpost->date()->toDate('F') ?></h3>
      <?php endif ?>
      <p><span class="date"><?= $blogpost->date()->toDate('d') ?> &mdash;</span> <a href="<?= $blogpost->url() ?>"><?= $blogpost->title() ?></a></p>
   <?php endforeach ?>

Note: for the month variable, make sure it's a unique variable year-over-year, so you never ever have to worry about some months not showing up because they match from the last post even though it's from a different year. I like the month number and year squished together: toDate('mY').

This could very well be how everyone has done this forever, or there might be a simpler way to do it that I have altogether failed to think of, but after several unfruitful Google searches, this is what I came up with and it works a treat.

Leave a comment down below

I think? webmentions and comments are all set up on the blog. On the receiving end, anyway — I couldn't get the Sendmentions plugin to work to send out webmentions automatically when I make a new post. I suppose I can handle that manually for now. Anyway, now you can leave a comment like it's 2009 :)

Hello, Kirby

Over the years, I've built my personal website with pretty much every CMS tool available. I basically learned PHP with WordPress, but at a certain point, it just became too big for what I would ever need. I've used static site generators like Eleventy and Jekyll, and while they are excellent tools, they require a little too much effort to add a new post. Most recently I tried building a blog using Notion and Super — Notion is my favorite tool at work, so why not use it for my blog, too? This came with its own limitations, and ~$15/month for Super was difficult to commit to, especially when I have already paid for my own web hosting for years to come.

I don't want for much with my website; I just want somewhere to post things online. It needs to be easy, though, because sometimes I want to post from my iPad, or my phone, and I definitely don't want to have to open up terminal or an FTP application to add something to my website. There are lots of self-hosted website builders out there, but they're all either too bloated or complicated or limited or just... not nice. Or, like in the case of Pixieset, they're just too specialized — I'm not running a photography blog, and until we have RSS support and customizable blocks it's just not likely to be the right fit for me.

I was looking at CMS options and dreading what felt like the inevitable return to WordPress when I came across Kirby. It's free to use, though purchasing a license is encouraged.

If I end up sticking with it, I have no problem paying for it; the one time payment is guaranteed to work out to far less than any self-hosted, monthly option, and so far Kirby is an absolute delight. It's as powerful as WordPress, flexible, and free of all of the bloat and crap that comes along with even the most bare-bones WordPress installation. Importantly, you can customize the admin panel — which I can easily use from my iPad or phone — to making posting simple. I'm sure all of this is possible with WordPress (it seems anything is possible with WordPress) but hell if it wouldn't be the most ridiculous process. I gave WordPress a try, in good faith, a few months back, and the entire experience was somehow both convenient and nightmarish at once. Kirby is just convenient.

There are, as always, still a million things to do before my website is 'finished' by any means. Building my templates from scratch means I have to figure things out along the way, and I'm learning a lot as I go. In the meantime, though, I'm pretty happy with how things are turning out!

First I was all whoa cool, so many CMS’ these days… it’s gonna have to be f*cking Wordpress isn’t it