Docs: Posts

Posts

The Blog's posts in a theme: posts, post, postCategories and postCategory, and how to link to the Blog itself.

These four calls read the Blog. They need the Blog module to be on.

posts($query = []) #

Live posts in the pages() envelope (E). Each item is {id, title, url, excerpt, image, published, updated, tags, author, author_url, category, current}.

  • Options: limit (1 to 50, default 10), page, offset, tag (a tag as an item's tags holds it), exclude (post ids, or 'self' for the post shown), order (desc by default, or asc) and category (since 1.19.0: a category's address, made of lowercase letters, digits and single hyphens, at most 100 characters; anything else gives error: 'category'). An unknown option gives error: 'option'.
  • A draft, or a post that is not yet due, is never listed, not even in a preview.
  • author is the author's display name, or an empty string when they set none. It is never their handle or an id.
  • author_url (since 1.19.2) is the author's profile page, escaped. It is an empty string unless the post names an author and the person looking may open that profile. Link the name when it is set. Print it plain when it is not.
  • tags is a list of escaped strings.
  • category (since 1.19.0) is {name, address, url}, escaped, or null for a post with no category.
  • image is judged as picture() judges it, so a cover that is not a same-site /uploads/ path or an https address is an empty string.
  • published and updated are Y-m-d H:i:s in UTC, for date(). updated is an empty string when unknown.
  • With the Blog off you get error: 'module' and no items.
<?php $latest = $this->hasModule('blog') ? $this->posts(['limit' => 3])['items'] : []; ?>
<?php foreach ($latest as $p): ?>
  <h3><a href="<?= $p['url'] ?>"><?= $p['title'] ?></a></h3>
  <time datetime="<?= $this->date($p['published'], 'iso') ?>"><?= $this->date($p['published'], 'short') ?></time>
<?php endforeach; ?>

post() #

The post this page shows, as a posts() item plus body (E, with body M), else null on every other render. It is the one place the previewed post shows. The body gets heading anchors (h2 to h4) while the Blog's "Links on headings" setting is on. Style .heading-anchor.

<?php if ($post = $this->post()): ?><h1><?= $post['title'] ?></h1><?= $post['body'] ?><?php endif; ?>

postCategories() #

New in 1.19.0. The Blog's categories in the order the site owner set (E). Each is {id, name, address, url, description, count, current}. count is its live posts, and current is true for the category this page shows. A post with no category is Uncategorised, which is never listed. With the Blog off you get [].

<?php foreach ($this->postCategories() as $c): ?>
  <a href="<?= $c['url'] ?>"<?= $c['current'] ? ' aria-current="page"' : '' ?>><?= $c['name'] ?></a>
<?php endforeach; ?>

postCategory() #

New in 1.19.0. The category this page shows, which is the Blog's category page, as a postCategories() item (E). It is null on every other page.

<?php if ($cat = $this->postCategory()): ?><h1><?= $cat['name'] ?></h1><?php endif; ?>

A post's own fields #

New in 1.20.2. Blog settings choose the Groups every post carries, and each category adds its own, so a post's fields follow its category. On the post's own page, field(), rows() and hasField() read the post's values exactly as they read a page's, escaped by each field's type (see Fields and pictures).

In a list, posts(['fields' => ['summary', 'rating']]) adds fields and option_values to each item, by the rules of pages()' fields option: up to 20 keys, a relation to pages as ids. A field that post does not have, because of its category, is absent from its fields. An unknown key, a message or a relation to posts gives error: 'fields'. Without the option an item has neither key, and post() never has them.

<?php if ($this->hasField('rating')): ?><p>Rating: <?= $this->field('rating') ?></p><?php endif; ?>

Reactions on a post #

New in 1.20.0. Members can answer a post with an emoji. posts() and post() do not hold the reactions. The Blog hands its post view a $reactions row and a $view_as flag, and the Blog's own views/post.php draws them with core's reactions component. A theme with its own views/post.php shows reactions only once it does the same.

<?php if (is_array($reactions ?? null)) { $this->component('reactions', ['reactions' => $reactions, 'inert' => !empty($view_as)]); } ?>

$reactions is null in a preview, and while reactions are off for the post and nobody has reacted. Reactions left before they were turned off stay shown, read only.

Share on a post #

Since 1.21.0 the Blog's blog-share component is a wrapper around core's share component. The old blog-share classes stay beside the new cms-share* ones, so existing styles still match. Three things to check in a theme:

  • /modules/blog/assets/blog-share.js no longer exists and gives a 404. Remove the line that loads it.
  • Set --cms-accent. Without it the buttons are blue instead of the old green.
  • The Blog's old share text keys are unused.

The forum topic of a post #

New in 1.21.0. When a post has a forum topic, the Blog's blog-discussion component links to it. It takes one parameter, discussion, which is {url, replies}: url is raw and root-relative, replies is a whole number. The Blog's view calls the component only when there is something to show, and Forums decide who sees it. A theme with its own views/post.php calls it the same way. Because the answer depends on the visitor, the page is kept out of the page cache.

<?php if (is_array($discussion ?? null)) { $this->component('blog-discussion', ['discussion' => $discussion]); } ?>

Linking to the Blog itself #

The site owner can rename the Blog and move its address. Never write /blog in a view. Ask section('blog') for the address, and test for null so a site that turned the Blog off shows no dead link. While the Blog is the site's home page, section('blog') still gives the Blog's own address, which leads to / with a permanent redirect, so the link keeps working. A post's address does not change. See Actions and sections.

<?php $blog = $this->section('blog'); ?>
<?php if ($blog !== null): ?><a class="more" href="<?= $blog['url'] ?>">All posts</a><?php endif; ?>