blogs - 十月 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.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"
解决方案
看来你没有提供开箱即用的东西
我创建了可以完成这项工作的片段。
在您
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
并仅显示该类别的帖子
如有任何疑问,请发表评论。
推荐阅读
- flutter - 如何在 Streambuilder 中控制 Stream(启动、暂停、恢复)
- ios - UITextField 在输入数字的最后添加 3 个零阻止输入
- javascript - 数据未在容器中使用 React-Redux(来自 mapStateToProps)传递到组件视图
- wordpress - 使用 Certbot 后如何将 WordPress 恢复到 NGINX?
- python - 如何将 django 模型导入外部 python 脚本
- python - 如何将此 Python 变量分配给我正在循环的列表?
- python - 纸浆最小化选择的加权平均值
- google-cloud-run - 如何从 Cloud Run 提供预压缩文件?
- python - 如何知道python中函数和方法的隐藏代码
- python - 如何从另一个 python 脚本启动 Django 应用程序