【问题标题】:October CMS (Rainlab Blog) - Next and Prev Post link from same category十月 CMS(Rainlab 博客)- 来自同一类别的下一个和上一个帖子链接
【发布时间】:2018-11-22 04:48:05
【问题描述】:

我有一个博客文章页面,我在其中使用类别作为 url 中的硬编码参数。

网址是这样的

url = "/category1/:slug"

我在这个布局中使用 blogPost 组件。 我可以使用模板中的以下代码获取nextPostprevPost 链接

{% set nextPost = blogPost.post.nextPost %}
{% set prevPost = blogPost.post.previousPost %}

但我想限制nextPostprevPostblogPost.post 属于同一类别,即category1

blogPost.post 只属于一个类别

我检查了Post 模型有一个方法scopeFilterCategories 但我不确定如何使用它或它是否有相同的用途。

代码

这是配置部分

title = "Category1 post"
url = "/category1/:slug"
layout = "default"
is_hidden = 0
robot_index = "index"
robot_follow = "follow"

[blogPost]
slug = "{{ :slug }}"
categoryPage = "category1"

【问题讨论】:

    标签: blogs octobercms octobercms-plugins


    【解决方案1】:

    您似乎没有提供开箱即用的功能

    我创建了 sn-ps 可以完成这项工作。

    在你page's code block添加这个代码sn-p [next previous工作基于published_at字段(在表单上发布)]

    public function nextPost($post) {
    
        // get current cats
        $postCats = $post->categories->pluck('id')->toArray();
    
        // Here you need to pass it as we are 
        // hardcoding category slug in URL so we have no data of category
    
        // IF YOU DONT WANT CAT COME FROM POST
        // YOU CAN HARD CODE THEM $postCats = ['2'] 
        // here 2 is id of category
    
        // use this cats to scope 
        $nextPost = $post->isPublished()->applySibling(-1)
                       ->FilterCategories($postCats)->first();
    
        // check if next is not availabe then return false
        if(!$nextPost) {        
            return false;
        }
    
        // create page link here same page
        $postPage = $this->page->getBaseFileName();
    
        // set URl so we can direct access .url
        $nextPost->setUrl($postPage, $this->controller);
    
        // set Cat URl so we can use it directly if needed
        $nextPost->categories->each(function($category) {
            $category->setUrl($this->categoryPage, $this->controller);
        });
    
        return $nextPost;
    
    }
    
    public function previousPost($post) {
    
        // get current cats
        $postCats = $post->categories->pluck('id')->toArray();
    
        // IF YOU DONT WANT CAT COME FROM POST
        // YOU CAN HARD CODE THEM $postCats = ['2'] 
        // here 2 is id of category
    
        // use this cats to scope 
        $prevPost = $post->isPublished()->applySibling(1)
                       ->FilterCategories($postCats)->first();
    
        // check if nprevious ext is not availabe then return false
        if(!$prevPost) {        
            return false;
        }
    
        // create page link here same page
        $postPage = $this->page->getBaseFileName();
    
        // set URl so we can direct access .url
        $prevPost->setUrl($postPage, $this->controller);
    
        // set Cat URl so we can use it directly if needed
        $prevPost->categories->each(function($category) {
            $category->setUrl($this->categoryPage, $this->controller);
        });
    
        return $prevPost;
    
    }
    

    您可以在Markup area 中添加此代码。

    {% component 'blogPost' %}
    
    {% set nextPostRecord = this.controller.pageObject.nextPost(blogPost.post) %}
    {% set previousPostRecord = this.controller.pageObject.previousPost(blogPost.post) %}
    
    {% if previousPostRecord %}
        <a href="{{ previousPostRecord.url }}"> Previous </a>    
    {% endif %}
    
    {% if nextPostRecord %}
        <a href="{{ nextPostRecord.url }}"> Next </a>    
    {% endif %}
    

    这将尊重 category 并仅显示该类别的帖子

    如有疑问请评论。

    【讨论】:

    • 我在{% set nextPostRecord = this.controller.pageObject.nextPost(blogPost.post) %} 的模板渲染部分收到错误“尝试获取非对象的属性”。当我转储 this.controller 时,它不显示任何 pageObject 命名方法或变量
    • 好的,我想通了。我将语句更改为{% set pageObject = this.controller.getPageObject() %} {% set nextPostRecord = pageObject.nextPost(blogPost.post) %} {% set previousPostRecord = pageObject.previousPost(blogPost.post) %}
    • 还有另一个问题。使用这些链接进行导航时,在某一点上,它们只能在两个帖子之间导航。在帖子“M”上按prev进入帖子“N”,在帖子“N”上按prev进入帖子“M”并不断重复。这背后的原因可能是什么。当我也使用默认的 nextPost 和 prevPost 方法时,就会出现这个问题。你有没有机会知道为什么会这样?顺便说一句,感谢 sn-ps :)
    • 当多个帖子在同一日期发布时,published_at 似乎搞砸了。
    • 是的,我忘了提及内部逻辑仅适用于不同的日期,我也遇到了同样的问题,但是当我更改日期时,它的工作正常......我猜你只是从后模型中提取逻辑并写入代码部分。您自己更正的逻辑,它应该可以工作
    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多