【问题标题】:View the first post differently, and the others all the same OCTOBERCMS (Rainlab Blog)以不同的方式查看第一个帖子,其他的都一样 OCTOBERCMS(Rainlab 博客)
【发布时间】:2020-11-28 14:39:03
【问题描述】:

我需要有关此问题的帮助,我想在一个部分查看第一篇文章,在另一个部分查看其他文章。我的这个解决方案不起作用。

我认为这需要一些条件。

类似的,在这里给我看最新的帖子,在这里给我看其他的....

我的组件

      {% set posts = __SELF__.posts %}       
        <section id="uvodniblog" class="divider">
    <div class="bt_feature_wrapper"> 
<!-- only one latest post -->
   {% for post in posts %}
    <div class="bt-feature-content bt_feature_main">
                        {% for image in post.featured_images|slice(0, 1) %} <div class="bt_item_img" style="background-image: url('{{ image.path }}');"></div> {% endfor %}
                        <a href="{{ post.url }}" class="bt_item_link" tabindex="-1"></a>
                        
                        <div class="slide_text_box">                    
                            <div class="meta-info-bg-color" style="color:w">
                                <a href="#" tabindex="-1" style="color:white;">..</a>
                            </div>
                            <h3>
                                <a href="{{ post.url }}" tabindex="-1">{{ post.title }}</a>
                            </h3>                                                                  
                             <div class="meta-info">                            
                                <ul>
                                    <li class="post-date">..</li>
                                </ul>
                            </div>
                        </div>
                    </div> {% endfor %} 
    
<!-- other post -->   
  {% for post in posts %}
    <div class="bt-feature-content bt-feature-small">
                        {% for image in post.featured_images|slice(0, 1) %} <div class="bt_item_img" style="background-image: url('{{ image.path }}');"></div>{% endfor %}
                        <a href="{{ post.url }}" class="bt_item_link" tabindex="-1"></a>
                        
                        <div class="slide_text_box">                    
                            <div class="meta-info-bg-color">
                                <a href="#" tabindex="-1" style="color:white;">..</a>
                            </div>
                            <h3>
                                <a href="{{ post.url }}" tabindex="-1">{{ post.title }}</a>
                            </h3> 
    
                            <div class="meta-info">                            
                                <ul>
                                    <li class="post-date">..</li>
                                </ul>
                            </div>                                                                 
                        </div>                     
                    </div>  {% endfor %} 
                    </div>
                    </div></section>

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    试试这个。

    
        {% set posts = __SELF__.posts %}       
        <section id="uvodniblog" class="divider">
            <div class="bt_feature_wrapper"> 
                <!-- only one latest post -->
                {% set latest = posts.last %}
                <div class="bt-feature-content bt_feature_main">
                    {% for image in latest.featured_images|slice(0, 1) %} 
                        <div class="bt_item_img" style="background-image: url('{{ image.path }}');"></div>
                    {% endfor %}
                    <a href="{{ latest.url }}" class="bt_item_link" tabindex="-1"></a>
                    
                    <div class="slide_text_box">                    
                        <div class="meta-info-bg-color" style="color:w">
                            <a href="#" tabindex="-1" style="color:white;">..</a>
                        </div>
                        <h3>
                            <a href="{{ latest.url }}" tabindex="-1">{{ latest.title }}</a>
                        </h3>                                                                  
                         <div class="meta-info">                            
                            <ul>
                                <li class="post-date">..</li>
                            </ul>
                        </div>
                    </div>
                </div>
                
                <!-- other post -->   
                {% for post in posts %}
                    {% if post.id != latest.id %}
                        <div class="bt-feature-content bt-feature-small">
                            {% for image in post.featured_images|slice(0, 1) %}
                                <div class="bt_item_img" style="background-image: url('{{ image.path }}');"></div>
                            {% endfor %}
                            <a href="{{ post.url }}" class="bt_item_link" tabindex="-1"></a>
                            
                            <div class="slide_text_box">                    
                                <div class="meta-info-bg-color">
                                    <a href="#" tabindex="-1" style="color:white;">..</a>
                                </div>
                                <h3>
                                    <a href="{{ post.url }}" tabindex="-1">{{ post.title }}</a>
                                </h3> 
        
                                <div class="meta-info">                            
                                    <ul>
                                        <li class="post-date">..</li>
                                    </ul>
                                </div>                                                                 
                            </div>                     
                        </div> 
                    {% endif %}
                {% endfor %} 
            </div>
        </section>
    
    

    【讨论】:

      【解决方案2】:

      您可以根据如下变量创建条件。

      {% set posts = __SELF__.posts %}    
      {% set latestPostId = 0 %}
      
      {# this part will render first post #}
      {% for post in posts %}
        {% if latestPostId == 0 %}
           {% set latestPostId = post.id  %}
           {# HERE you can use markup for first posts #}
        {% endif%}
      {% endfor %}
      
      {# this part will render all other post #}
      {% for post in posts %}
        {% if latestPostId != post.id %}
           {# HERE you can use markup for other posts #}
        {% endif%}
      {% endfor %}
      

      如有疑问请留言

      【讨论】:

      • 我想我的代码成功了。只是 .. 是否可以禁用其他帖子中的介绍性内容?
      • 我不确定我不明白你的要求,你能分享更多细节
      【解决方案3】:

      您应该能够直接获取最后一篇文章,而不是遍历第一部分的所有帖子:

      {% set lastPost = posts.last %}
      

      对于循环,你可以跳过这篇文章:

      {% for post in posts if post.id != lastPost.id %}
      ...
      {% endfor %}
      

      【讨论】:

        【解决方案4】:

        第一次发帖:

        {% set post = posts|first (or posts.first)
        

        剩余帖子:

        {% for post in posts %}
            {% if loop.index != 1 %}
                ...
            {% endif %}
        
        {% endfor %}
        

        【讨论】:

          猜你喜欢
          • 2018-06-17
          • 2019-08-24
          • 2017-05-23
          • 1970-01-01
          • 2019-06-24
          • 2018-11-22
          • 2019-07-02
          • 2021-05-24
          • 2019-07-25
          相关资源
          最近更新 更多