Search code examples
phplaraveloctobercmsoctobercms-pluginsoctober-form-controller

RainLab blog - OctoberCMS - Show posts from specific category


I've found the following code in Posts.php - I think I essentially need to duplicate this and turn it into a public function limiting the category to a sepcific ID. Although that seems overkill? Can I query the current function on the front end?

Here's the posts.php code:

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;
}

on the front end is this:

{% for post in posts %}
    <li>
        <h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
    </li>
{% else %}
    <li class="no-data">{{ noPostsMessage }}</li>
{% endfor %}

Could someone point me in the right direction?

Cheers,


Solution

  • Not sure what you want to achieve. You just want to show posts from specific category only? Set the parameter "category filter" of your blogPosts component to the slug of specific category

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

    this will show posts only from "news" category