Php RainLab博客-十月CMS-显示特定类别的帖子

Php RainLab博客-十月CMS-显示特定类别的帖子,php,laravel,octobercms,octobercms-plugins,october-form-controller,Php,Laravel,Octobercms,Octobercms Plugins,October Form Controller,我在Posts.php中发现了以下代码-我认为我需要复制它,并将其转换为一个公共函数,将类别限制为sepcific ID。尽管这似乎有些过分了?我可以在前端查询当前函数吗 下面是posts.php代码: protected function listPosts() { $category = $this->category ? $this->category->id : null; /* * List all the posts, eager loa

我在Posts.php中发现了以下代码-我认为我需要复制它,并将其转换为一个公共函数,将类别限制为sepcific 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 %}
{%for posts in posts%}
  • {%else%}
  • {{noPostsMessage}
  • {%endfor%}
    有人能给我指出正确的方向吗


    干杯,

    不确定你想要实现什么。您只想显示特定类别的帖子? 将blogPosts组件的参数“category filter”设置为特定类别的slug

    [blogPosts newsList]
    pageNumber = "{{ :page }}"
    categoryFilter = "news"
    postsPerPage = 100
    noPostsMessage = "No posts found"
    sortOrder = "published_at desc"
    categoryPage = "blog/category"
    postPage = "blog/post"
    ==
    {% component 'newsList' %}
    

    这将显示仅来自“新闻”类别的帖子

    在再次阅读该插件的文档后偶然发现了这一点。谢谢