1

This is for an index page (again). I want to list the 10 most recent articles. There is a field postImage. It is not required. If it exists, I want to use postImage. If not, I want to use a default image.

The relevant code is under the img tag.

I get the following error: Impossible to invoke a method ("getUrl") on a null variable.. I can verify that 7 of the first 10 entries do have a postImage.

{% for item in craft.entries.section('posts').limit(10) %} {% set pic = item.postImage.one().getUrl() %} {% if loop.index is divisible by(2) %}

                        <div class="card card-plain card-blog">
                            <div class="row">
                                <div class="col-md-5">
                                    <div class="card-header card-header-image">
                                        <a href="#pablito">

                                            <img class="img img-raised" src="{% if pic %}{{ pic }}{% else %}assets/img/office2.jpg
                                                {% endif %}
                                            ">    

                                        </a>
                                    </div>
                                </div>
                                <div class="col-md-7">
                                    <h6 class="card-category text-info">Enterprise</h6>
                                    <h3 class="card-title">
                                        <a href="{{ item.url }}">{{ item.title }}</a>
                                    </h3>
                                    <p class="card-description">
                                       {{ item.introParagraph }}
                                        <a href="{{ item.url }}"> Read More </a>
                                    </p>
                                    <p class="author">
                                        by
                                        <a href="">
                                            <b>x</b>
                                        </a>, 2 days ago
                                    </p>
                                </div>
                            </div>
                        </div>

                        {% else %}

There are 2 if / else statements. Only the 1 associated with the img tag is relevant.

Thanks.

1 Answer 1

1

Drop the getUrl() from the set and everything works.

Your Answer

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

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