diff --git a/src/classes/blog.php b/src/classes/blog.php index b4d6ac8b..be05e1e4 100644 --- a/src/classes/blog.php +++ b/src/classes/blog.php @@ -42,6 +42,27 @@ class Blog throw new \Exception('No post found with the slug "' . $slug . '".'); } + public static function getPreviousCurrentNextPostBySlug(string $slug): array + { + $posts = self::get(self::ENDPOINT_POSTS); + + for ($i = 0; $i < count($posts); $i++) { + $previous = $posts[$i - 1] ?? null; + $current = $posts[$i] ?? null; + $next = $psots[$i + 1] ?? null; + + if ($slug === $current->slug) { + return array( + 'previous' => $previous, + 'current' => $current, + 'next' => $next, + ); + } + } + + throw new \Exception('No post found with the slug "' . $slug . '".'); + } + public static function getPosts(): array { $posts = self::get(self::ENDPOINT_POSTS); diff --git a/src/pages/post.php b/src/pages/post.php index c5383a40..27b05d3e 100644 --- a/src/pages/post.php +++ b/src/pages/post.php @@ -9,7 +9,8 @@ namespace wishthis; $postSlug = $_SESSION['_GET']['slug']; -$post = Blog::getPostBySlug($postSlug); +$posts = Blog::getPreviousCurrentNextPostBySlug($postSlug); +$post = $posts['current']; $postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : ''; $postMedia = isset($post->featured_media) ? Blog::getMedia($post->featured_media) : new \stdClss(); @@ -47,8 +48,8 @@ $page->navigation();
-
+