octobercmsoctobercms-plugins

OctoberCMS Blog plugin with Recent post


I have a strange requirement from the client. I am using OctoberCMS Blog plugin https://octobercms.com/plugin/rainlab-blog and its working as per my expectations.

However, I have a section called Recent Post in which I am showing 1 latest recent post and a blog listing in the same page with blogs list with latest coming as first blog.

Now my client is saying, the blog in Recent Post is same as first blog which is a latest blog, which is obviously the same.

Hence I used Twig's slice function https://twig.symfony.com/doc/3.x/filters/slice.html to hide/remove first blog from the list. Here is how I did it.

 {% if this.page.id == 'blog' %}
    {% if posts.currentPage == 1 %}
      {% set sliceBlogs = 1 %}
    {% else  %}
      {% set sliceBlogs = 0 %}
    {% endif %}
  {% else %}
    {% set sliceBlogs = 0 %}
  {% endif %}

{% for post in posts | slice(sliceBlogs) %}

Here as you can see, I have made a condition, If Only blog listing page is found and also first page is found, then only it will hide/remove first blog from the blog list.

But here I am having issues at Post per page. Currently I have set as 11 but when its on first page on blog listing, because I have sliced/skipped first blog, its showing one less which is not correct ..

So my question is, how can I make this part proper and make sure it will show same number of maximum blogs on each page ?

Thanks


Solution

  • OK guys, eventually I have resolved in this way ..

    I put below code in my Blog CMS page ..

    function onStart()
    {       
        
        $latestPost = RainLab\Blog\Models\Post::isPublished()->orderBy('published_at', 'desc')->first();
        $this->blogPosts->setProperty('exceptPost', $latestPost->id);
    }
    

    It will remove the first latest post from the blog list.

    Thanks