【问题标题】:RainLab blog - OctoberCMS - Show posts from specific categoryRainLab 博客 - OctoberCMS - 显示特定类别的帖子
【发布时间】:2018-06-17 08:42:20
【问题描述】:

我在 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 %}

有人能指出我正确的方向吗?

干杯,

【问题讨论】:

    标签: php laravel octobercms octobercms-plugins october-form-controller


    【解决方案1】:

    不确定您想要实现什么。您只想显示特定类别的帖子? 将 blogPosts 组件的参数“类别过滤器”设置为特定类别的 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' %}
    

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

    【讨论】:

    • 在再次阅读插件的文档后,字面上只是偶然发现了这一点。谢谢
    猜你喜欢
    • 2017-05-23
    • 2019-08-24
    • 2019-07-02
    • 2020-02-25
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多