3

I have this this code -

{% for entry in craft.entries.section('news').first() %}
    <div class="container" id="featured-news">
        <div class=col-md-6>
            <h2 class="news-title">{{ entry.title }}</h2>
            <h3 class="news-date">{{ entry.postDate.format('F d, Y') }}</h3>

            <p>
                {{ entry.body }}
            </p>
        </div>

        <img class="col-md-4" src="images/pic-block-pic1.jpg" style="float:right;">

        <div class="col-xs-12" >
            <a href="{{ entry.url }}" style="float:right; padding: 20px 0px 20px 0px">READ FULL STORY...</a>
        </div>
    </div>
{% endfor %}

I thought the .first() would pick up just the first news story, however it is returning the same container several times.

I then want to return all other entries elsewhere, so what should I include in my code below to do this -

<div class="imgOverview">
    <ul>
        {% for entry in craft.entries.section('news').find() %}
            <li>
                <div><img src="images/pic-block-pic1.jpg" alt=""><div class="imgOverviewOverlay"><a href="{{ entry.url }}"><span>{{ entry.title }}<small>{{ entry.postDate.format('F d, Y') }}</small></span></a></div></div>
            </li>
        {% endfor %}    
    </ul>
</div>

1 Answer 1

5

As you'll only ever be working with one entry I wouldn't bother with the for loop for the first part of your question. Just set a variable with your featured news article. Something like this (not tested though):

{% set featuredArticle = craft.entries.section('news').first() %}

<div class="container" id="featured-news">
    <div class=col-md-6>
        <h2 class="news-title">{{ featuredArticle.title }}</h2>
        <h3 class="news-date">{{ featuredArticle.postDate.format('F d, Y') }}</h3>
        <p>
        {{ featuredArticle.body }}
        </p>
    </div>
    <img class="col-md-4" src="images/pic-block-pic1.jpg" style="float:right;">
    <div class="col-xs-12" >
    <a href="{{ featuredArticle.url }}" style="float:right; padding: 20px 0px 20px 0px">READ FULL STORY...</a>
    </div>
</div>

EDIT: Then for the second part of your question this other answer tells you how to exclude an Entry:

How can I exclude the current entry when I've structured my syntax like this?

So from the other article, you'd do:

{% set entries = craft.entries({
  section: 'news'
}).id('not ' ~ featuredArticle.id) %}
2
  • 2
    Thanks very much! That worked a treat. However the other question you linked me didn't help much. I looked at Craft Docs and found .offset(). Where I can .offset(1) and exclude the first entry from being returned.
    – Richard
    Commented Jun 16, 2016 at 12:13
  • 1
    The other answer I linked gives you much more control, you are explicitly telling it which entry you don't want. Later down the line you might decide that rather than it just being the first news article you want to be featured, you might choose a particular article, offset won't help in that instance. Commented Jun 16, 2016 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.