首页 > 解决方案 > 十月 CMS(Rainlab 博客)- 来自同一类别的 Next 和 Prev Post 链接

问题描述

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

网址就像

url = "/category1/:slug"

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

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

但我想限制nextPost并与ieprevPost属于同一类别blogPost.postcategory1

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"

标签: blogsoctobercmsoctobercms-plugins

解决方案


看来你没有提供开箱即用的东西

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

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

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


推荐阅读