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

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

                        {% else %}

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


1 Answer 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 you have read our privacy policy.

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