10月CMS(Rainlab博客) - 来自同一类别的Next和Prev Post链接

时间:2018-06-12 17:54:16

标签: blogs octobercms octobercms-plugins

我有一个博客帖子页面,我在url中将该类别用作硬编码参数。

网址就像

url = "/category1/:slug"

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

中的以下代码获取nextPostprevPost链接
{% set nextPost = blogPost.post.nextPost %}
{% set prevPost = blogPost.post.previousPost %}

但我想约束nextPostprevPost来自与blogPost.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"

1 个答案:

答案 0 :(得分:2)

似乎Thye没有提供开箱即用的

我创建了片段,可以完成这项工作。

  

在你page's code block中添加此代码段[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并仅显示该类别帖子

如有任何疑问请发表评论。