58 lines
2.3 KiB
PHP
58 lines
2.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$post = null;
|
|
$slug = $_GET['slug'] ?? null;
|
|
|
|
if ($slug) {
|
|
try {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare('SELECT * FROM posts WHERE slug = ?');
|
|
$stmt->execute([$slug]);
|
|
$post = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
error_log('Failed to fetch post: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
if (!$post) {
|
|
http_response_code(404);
|
|
require_once __DIR__ . '/includes/header.php';
|
|
echo '<div class="container text-center my-5"><h1 class="display-1">404</h1><p class="lead">Article not found.</p><a href="blog.php" class="btn btn-primary">Back to Blog</a></div>';
|
|
require_once __DIR__ . '/includes/footer.php';
|
|
exit;
|
|
}
|
|
|
|
// SEO & Page Variables
|
|
$pageTitle = $post['seo_title'] ?? $post['title'];
|
|
$pageDescription = $post['seo_meta_description'] ?? substr(strip_tags($post['content']), 0, 160);
|
|
$og_image = $post['image_url']; // Assuming this is a full URL
|
|
$og_type = 'article';
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<main class="container my-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-8">
|
|
<article>
|
|
<header class="mb-4">
|
|
<h1 class="fw-bolder mb-1"><?php echo htmlspecialchars($post['title']); ?></h1>
|
|
<div class="text-muted fst-italic mb-2">
|
|
Posted on <?php echo date("F j, Y", strtotime($post['published_at'])); ?> by <?php echo htmlspecialchars($post['author']); ?>
|
|
</div>
|
|
</header>
|
|
<figure class="mb-4">
|
|
<img class="img-fluid rounded" src="<?php echo htmlspecialchars($post['image_url'] ?? 'https://picsum.photos/900/400'); ?>" alt="<?php echo htmlspecialchars($post['title']); ?>" />
|
|
</figure>
|
|
<section class="mb-5 fs-5">
|
|
<?php echo nl2br(htmlspecialchars($post['content'])); // Using nl2br and htmlspecialchars for basic formatting and security. For full HTML, a sanitizer would be needed. ?>
|
|
</section>
|
|
</article>
|
|
<a href="blog.php" class="btn btn-outline-primary">← Back to all articles</a>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|