Menu

1 / 2

Dynamic related articles in PHP without a database

DEV Community·Odilon HUGONNOT·6 months ago
#9gcTIQ7E
#php#nodatabase#slug#score#tags
Reading 0:00
15s threshold

The "related articles" section at the bottom of a blog post is exactly the kind of thing you hardcode at first — two links picked by hand — and then forget to maintain. The result: stale suggestions pointing to unrelated articles, or worse, articles that no longer exist.

On this blog, all content is described in a single posts.json file, with each article's slug, category, and tags. That's enough to automatically calculate relevant suggestions without a database.

The problem with hardcoded links

The initial version passed an array of links to blog_footer() :

<?php blog_footer ([ [ 'url' => '/blog/commentaires-sans-bdd-php' , 'title' => 'Adding comments...' ], [ 'url' => '/blog/creer-un-blog-avec-claude-code' , 'title' => 'Building a blog...' ], ]); ?>

Enter fullscreen mode

Exit fullscreen mode

Functional, but requires manual maintenance in every article file.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.