Try to learn something about everything, and everything about somethingThomas Huxley “Darwin's bulldog” (1824-1895)

This is an old revision of the document!


Computers

Pureblog

Description

Section One

Addons

tag-cloud.php
<?php
 
 
 
declare(strict_types=1);
 
 
 
$pageTitle = 'Tag Cloud';
 
$metaDescription = 'All tags, sized by how often they appear.';
 
 
 
$tagIndex = load_tag_index();
 
$tagCounts = [];
 
if ($tagIndex) {
 
    foreach ($tagIndex as $slug => $slugs) {
 
        $tagCounts[$slug] = count($slugs);
 
    }
 
}
 
 
 
// Collect original display names from posts (slugs alone lose capitalisation/spaces)
 
$originalNames = [];
 
foreach (get_all_posts(false) as $post) {
 
    foreach (($post['tags'] ?? []) as $tag) {
 
        $slug = normalize_tag($tag);
 
        if (!isset($originalNames[$slug])) {
 
            $originalNames[$slug] = $tag;
 
        }
 
    }
 
}
 
 
 
// Sort alphabetically by display name
 
uksort($tagCounts, function (string $a, string $b) use ($originalNames): int {
 
    return strcasecmp($originalNames[$a] ?? $a, $originalNames[$b] ?? $b);
 
});
 
 
 
$maxCount = $tagCounts ? max($tagCounts) : 1;
 
$minCount = $tagCounts ? min($tagCounts) : 1;
 
$range    = $maxCount > $minCount ? $maxCount - $minCount : 1;
 
 
 
require PUREBLOG_BASE_PATH . '/includes/header.php';
 
render_masthead_layout($config, ['page' => null]);
 
?>
 
<main>
 
    <h1>Tag Cloud</h1>
 
 
 
    <?php if (empty($tagCounts)): ?>
 
        <p>No tags found.</p>
 
    <?php else: ?>
 
        <p class="tag-cloud">
 
            <?php foreach ($tagCounts as $slug => $count):
 
                $name     = $originalNames[$slug] ?? $slug;
 
                $ratio    = ($count - $minCount) / $range;
 
                $fontSize = round(0.85 + $ratio * 1.4, 2);
 
                $postWord = $count === 1 ? 'post' : 'posts';
 
                echo '<a href="/tag/' . e(rawurlencode($slug)) . '"'
 
                   . ' style="font-size: ' . $fontSize . 'em"'
 
                   . ' title="' . e((string) $count) . ' ' . $postWord . '">'
 
                   . e($name) .  '(' .  e((string) $count) . ')' .  '</a>' . '&emsp;&emsp;';
 
            endforeach; ?>
 
        </p>
 
    <?php endif; ?>
 
</main>
 
<?php render_footer_layout($config, ['page' => null]); ?>
 
</body>
 
</html>
archive.php
<?php
declare(strict_types=1);
 
$allPosts = get_all_posts(false);
$postsByYear = [];
 
foreach ($allPosts as $entry) {
    $year = !empty($entry['date']) ? date('Y', strtotime((string) $entry['date'])) : 'Unknown';
    $postsByYear[$year][] = $entry;
}
 
krsort($postsByYear, SORT_NATURAL);
 
$pageTitle = 'Archive';
$metaDescription = 'Browse all published posts by year.';
?>
<?php require PUREBLOG_BASE_PATH . '/includes/header.php'; ?>
<?php render_masthead_layout($config); ?>
<main class=archive>
    <article>
        <h1>Archive</h1>
        <p><?= e((string) count($allPosts)) ?> published posts.</p>
 
        <?php if (!$allPosts): ?>
            <p>No published posts yet.</p>
        <?php else: ?>
            <?php foreach ($postsByYear as $year => $yearPosts): ?>
                <h3><?= e((string) $year) ?> (<?= e((string) count($yearPosts)) ?>)</h3>
                <ul>
                    <?php foreach ($yearPosts as $postItem): ?>
                        <li>
                            <?php if (!empty($postItem['date'])): ?>
                              <!--  <small> -->
                                    <time datetime="<?= e((string) $postItem['date']) ?>">
                                        <?= e(date('D Y-m-d H:i', strtotime((string) $postItem['date']))) ?>
                                    </time>
			   <!--     </small> -->
				&emsp;
                            <?php endif; ?>
                            <a href="/<?= e((string) ($postItem['slug'] ?? '')) ?>">
                                <?= e((string) ($postItem['title'] ?? 'Untitled')) ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endforeach; ?>
        <?php endif; ?>
    </article>
</main>
<?php render_footer_layout($config); ?>
</body>
</html>

Further Information

Page created : 02/03/26 08:50 GMT

Page updated : 02/03/26 08:54 GMT


Navigation