2017-01-09 3 views
0

10月のCMSでRain Lab Postを使用しています。 blogPostコンポーネントを使用して問題はなく、1つのカテゴリから投稿を取得できます。例えば。これは、カテゴリOctoberCMS RainLabブログを使用して異なるカテゴリの投稿を結合する

[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> 

から最後の5ポストを示ししかし、今、私はホームページで働いているし、すべてのカテゴリから最後のポストを表示したい場所を部分的です。 1カテゴリにつき1つ、5カテゴリを組み合わせたものです。

誰かがこれを行う方法を知っていますか?事前

答えて

5

おかげで、私は次の方法でこれを解決:私はこのよう

{% 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

おかげで、あなたのページ/レイアウトのコードセクションに手動でデータをフェッチする必要がこの方法です。

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

もう1つの解決策は、通常のPHP array_mergeを使用することです。アーメドESSAMから最初のaproachに

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(); 
} 
+0

に使用できるテンプレートで

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

おかげで、あなたは私を与えます軽いですが、問題があります:category_idフィールドは他のテーブルにあります。 3つのテーブルがあります。 rainlab_blog_posts; rainlab_blog_categories; ; rainlab_blog_posts_categories; ; 使用: $ first_category_post = \ Rainlab \ Blog \ Models \ Post :: orderBy( '​​id'、 'desc') - >ここで( 'category_id'、1) - > first(); は、 'category_id'がrainlab_blog_posts_categories関係テーブルにあるため、エラーを返します。 – PolloZen

関連する問題