十月 CMS(Rainlab 博客)- 同一类别的下一个和上一个 Post link

October CMS (Rainlab Blog) - Next and Prev Post link from same category

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

url就像

url = "/category1/:slug"

我在此布局中使用了 blogPost 组件。 我可以在模板

中使用以下代码获取 nextPostprevPost link
{% 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"

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

我创建了 snippets 可以完成这项工作。

In you page's code block add this code snippet [ next previous works based on published_at field (Published on in form) ]

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;

}

In Markup area you can add this code.

{% 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 并仅显示该类别的帖子

如有疑问请评论。