我在 Posts.php 中找到了以下代码 - 我认为我基本上需要复制它并将其转换为将类别限制为特定 ID 的公共函数。虽然这看起来有点矫枉过正?可以在前端查询当前功能吗?
这是posts.php代码:
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
/*
* List all the posts, eager load their categories
*/
$isPublished = !$this->checkEditor();
$posts = BlogPost::with('categories')->listFrontEnd([
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('postsPerPage'),
'search' => trim(input('search')),
'category' => $category,
'published' => $isPublished,
'exceptPost' => $this->property('exceptPost'),
]);
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) {
$post->setUrl($this->postPage, $this->controller);
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
});
return $posts;
}
前端是这样的:
{% for post in posts %}
<li>
<h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
</li>
{% else %}
<li class="no-data">{{ noPostsMessage }}</li>
{% endfor %}
有人能指出我正确的方向吗?
干杯,