【问题标题】:Add category slug in post page在帖子页面中添加类别 slug
【发布时间】:2019-12-11 07:14:49
【问题描述】:

我是 10 月 cms 的初学者,并在我的个人博客中使用 Rainlab 博客插件。 我想在博文页面的url中显示category slug,我该如何实现呢?

即 example.com/{category_slug}/{blog_post_slug}

【问题讨论】:

    标签: content-management-system blogs octobercms slug


    【解决方案1】:

    应该只是example.com/:category?/:slug?

    除非您一起跳过该组件,否则无法解决。帖子的博客组件只是在寻找 slug::slug。在我看来,我认为您真的不必担心人们会改变类别。您的帖子列表页面应该有 url example.com/category/post 的链接。为什么用户会更改类别?

    如果你真的很在乎这里是一种解决方法。 不要使用博客文章组件。制作自己的插件或使用 CMS 页面。代码会略有不同,在此示例中,我将使用 CMS 页面代码,因为它是“临时”且更快。

    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();
    }
    

    【讨论】:

    • 那行得通,但即使我在类别部分写任何东西,它也会加载博客页面。 example.com/abc/blog-slug example.com/blog-category/blog-slug 都返回相同的博客页面,我认为这是不对的。
    • 这应该让我开始。谢谢
    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    相关资源
    最近更新 更多