Add pretty url for blog post

This commit is contained in:
grandeljay 2022-06-08 14:46:26 +02:00
parent 01c5e08fad
commit aea0773df8
6 changed files with 19 additions and 4 deletions

View file

@ -17,7 +17,7 @@
RewriteRule ^wishlist/([0-9a-f]{40})$ /?page=wishlist&hash=$1 [QSA,L]
# Blog Post
RewriteRule ^blog/([a-z\-0-9]+)$ /?page=blog&slug=$1 [QSA,L]
RewriteRule ^blog/([a-z\-0-9]+)$ /?page=post&slug=$1 [QSA,L]
</IfModule>
##-- When caching of gzipped JS and CSS files is used, enable this setting

View file

@ -33,6 +33,19 @@ class Blog
return $post;
}
public static function getPostBySlug(string $slug): \stdClass
{
$posts = self::get(self::ENDPOINT_POSTS);
foreach ($posts as $post) {
if ($slug === $post->slug) {
return $post;
}
}
throw new \Exception('No post found with the slug "' . $slug . '".');
}
public static function getPosts(): array
{
$posts = self::get(self::ENDPOINT_POSTS);

View file

@ -122,6 +122,7 @@ class Page
$ignorePower = array(
'home',
'blog',
'post',
'install',
'login',
'maintenance',

View file

@ -32,7 +32,7 @@ $posts = Blog::getPosts();
);
$mediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : '';
$categoriesHTML = Blog::getCategoriesHTML($post->categories);
$postLink = '/?page=post&id=' . $post->id;
$postLink = '/?page=post&slug=' . $post->slug;
?>
<div class="column">
<div class="ui fluid card stretch">

View file

@ -45,6 +45,7 @@ $page->navigation();
<ul>
<li><?= __('Localisation (many new translations added)') ?></li>
<li><?= __('Saved wish information is not updated if the url can not be found') ?></li>
<li><?= __('URL handling') ?></li>
</ul>
<h3 class="ui header"><?= __('Changed') ?></h3>

View file

@ -8,8 +8,8 @@
namespace wishthis;
$postID = $_SESSION['_GET']['id'];
$post = Blog::getPost($postID);
$postSlug = $_SESSION['_GET']['slug'];
$post = Blog::getPostBySlug($postSlug);
$postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : '';
$page = new Page(__FILE__, 'Post');