Search code examples
content-management-systemblogsoctobercmsslug

Add category slug in post page


I am beginner in october cms and using rainlab blog plugin for my personal blog. I want to display category slug in the url of blog post page, how can i achieve that?

i.e example.com/{category_slug}/{blog_post_slug}


Solution

  • Should just be example.com/:category?/:slug?

    Unless you skip the component all together there is no way around that. The Blog component for a post is just looking for the slug: :slug. I don't think you really have to worry about people changing the category in my opinion. Your post list page should have the url example.com/category/post for its link. Why would a user change the category?

    enter image description here

    If you really care here is a way around that. Don't use the blog post component. Make your own plugin or use CMS Pages. The code will vary slightly and in this example I will use CMS Page code cause it is 'ad hoc' and quicker.

    use Rainlab\Blog\Models\Post;
    function onStart() {
        $category = $this->param('category');
        $slug = $this->param('slug');
        $posts = Post::whereHas('categories', function ($query) use ($category) {
                $query->where('slug', $category);
            })->get();
        $this['post'] = $posts->where('slug', $slug)->first();
    }