【问题标题】:Get combined post from different categories using OctoberCMS RainLab Blog使用 OctoberCMS RainLab 博客获取来自不同类别的组合帖子
【发布时间】:2017-05-23 05:47:39
【问题描述】:

我在十月 CMS 中使用 Rain Lab Post。 我使用 blogPost 组件并从单个类别中获取帖子没有问题。例如。这是显示某个类别的最后 5 个帖子的部分内容

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

但是现在,我正在主页上工作,并希望显示所有类别的最后一篇文章。每个类别 1 个,5 个类别,合并。

有人知道怎么做吗?

提前致谢

【问题讨论】:

    标签: laravel octobercms octobercms-plugins


    【解决方案1】:

    感谢 Ahmed Essam 的第一个方法,我通过以下方式解决了这个问题:

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

    在模板中我可以这样使用

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

    【讨论】:

      【解决方案2】:

      这样您必须在页面/布局的代码部分手动获取数据。

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

      另一种解决方案是使用通常的 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();
      }
      

      【讨论】:

      • 谢谢,你给我亮了,但是有一个问题:category_id字段在其他表中。有 3 个表:rainlab_blog_posts; rainlab_blog_categories; rainlab_blog_posts_categories;使用:$first_category_post = \Rainlab\Blog\Models\Post::orderBy('id', 'desc')->where('category_id', 1)->first();返回错误,因为“category_id”在rainlab_blog_posts_categories 关系表中。
      猜你喜欢
      • 2018-06-17
      • 2019-08-24
      • 2019-06-24
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2019-07-25
      • 1970-01-01
      相关资源
      最近更新 更多