0
votes

I'm using Rain Lab Post in October CMS. I have no problem using the blogPost component and get post from a single category. E.g. This is a partial where show the last 5 post from a category

[blogPosts]
pageNumber = "{{ :page }}"
categoryFilter = "{{ slug }}"
postsPerPage = 5
noPostsMessage = "No news in this category"
sortOrder = "published_at desc"
categoryPage = 404
postPage = "singlePost"
==
<li> <a href="#">{{category.name}}</a>
    <ul class="drop-down full-width col-5 hover-expand">
        <li class="validation">
            <h2 class="mm-title">{{category.name}}</h2>
        </li>
        {% for post in posts %}
        <li>
            {% for image in post.featured_images|slice(0,1) %}
                <a href="{{ post.url }}"><img src="{{ image.path }}" alt=""></a>
            {% endfor %}
            <h3><a href="{{ post.url }}">{{post.title}}</a></h3>
        </li>
        {% endfor %}
    </ul>
</li>

But now, I'm working in the home page and want to display the last post from all categories. 1 per category, 5 categories, combined.

Somebody knows how to do this?

Thanks in advance

2

2 Answers

5
votes

Thanks to first aproach from Ahmed Essam, I resolve this in the next way:

function onStart(){
   $categories = Db::table('rainlab_blog_categories')->get();
   foreach ($categories as $key=>$category){
      if($category->slug != 'uncategorized'){
         $first = \Rainlab\Blog\Models\Category::with('posts')->where('id',$category->id)->first();
         $data[] = $first->posts[0];
      }
   }
   $this['posts'] = $data;
}

In the template I can use in this way

{% for post in posts %}
    <li class="col-md-6 col-sm-6">
        <div id="container">

            {% for image in post.featured_images | slice(0,1) %}
               <div class="thumb"><img src="{{image.path}}" alt=""></div>
            {% endfor %}

            <div class="content">

                {% for category in post.categories %}
                    <div class="cat">{{category.name}</div>
                {% endfor %}

                <h3><a href="{{ post.url }}">{{ post.title )}}</a></h3>
            </div>
        </div>
    </li>
{% endfor %}
0
votes

This way you have to fetch data manually in the code section of your page/layout.

function onStart() {
$first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
$second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
$third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
$fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
$fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();

$array = $first_category_post->merge($second_category_post);
$array = $array->merge($third_category_post);
$array = $array->merge($fourth_category_post);
$array = $array->merge($fifth_category_post);

$this['posts'] = $array;
}

Another solution is to use usual php array_merge.

function onStart() {
$first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();
$second_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 2)->first();
$third_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 3)->first();
$fourth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 4)->first();
$fifth_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 5)->first();

$array = array_merge($first_category_post->toArray(), $second_category_posts->toArray());
$array = array_merge($array, $third_category_post->toArray());
$array = array_merge($array, $fourth_category_post->toArray());
$array = array_merge($array, $fifth_category_post->toArray());

$this['posts'] = $array->toJson();
}