1

I want to get entries randomly every minutes like setTimeout in javascript. Like below If possible.

            setTimeout({ {% for entry in craft.entries.section('alumni').order('RAND()').limit(6).find() %}
            <a class="main-home-list-item" href="#">
                <div class="main-home-list-item__body">
                    {% set asset = entry.profileImage.first() %} {% if asset | length %}
                    <div class="img-container">
                        <img src="{{asset.getUrl()}}" alt="{{asset.title}}" />
                    </div>
                    {% else %}
                    <div class="img-container">
                        <img src="assets/avatar.png" />
                    </div>
                    {% endif %}
                    <h4>
                                {{ entry.title }} {{ entry.lastName }}
                            </h4>
                    <p class="small">
                        {{entry.graduateYear}}
                    </p>
                </div>
            </a>
            {% endfor %} }, 1000);
3
  • Twig is pre rendered on the server so I'm not sure what you're trying to accomplish here? Do you want to show a new randomly chosen entry each minute?
    – mrsmn
    Jul 24 '17 at 11:26
  • I want to get random entries from craft CP. like randomly showing images every minutes 6 at a time and looping through the others. Jul 24 '17 at 11:36
  • You'll have to output the html for all of them beforehand, in the order you choose, and than use javascript on the frontend to do the switching every minute. I'm en route at the moment, i'll try and whip the above into a proper answer when I get back.
    – mrsmn
    Jul 24 '17 at 12:04
1

See below for some twig code to get you started, I omitted the actual HTML output for the individual items to better keep the overview.

Whats happening is, you get all (or a reasonable big amount amount) of your entries, and start looping over them.

The first time, you open a div that will contain 6 items. Afterwards, each time the loop index can be divised by 6 (aka, 6 more items have been looped over) you close the container div & open a new one.

Than finally after the last iteration, you close the container div again.

Making the 6 items hide or display you can do with Javascript. Let me know if you need extra pointers on that.

{% for entry in craft.entries.section('alumni').order('RAND()')}
    {% if loop.first %}
        <div class="container-for-6-items">
    {% endif %}

    {# Build item output here #}

    {% if loop.index is divisible by(6) %}
        </div>
        <div class="container-for-6-items">
    {% endif %}

    {% if loop.last %}
        </div>
    {% endif %}
{% endfor %}

Info on divisible by

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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