1

I am brand new to Craft and can't find much support on this problem online. I have my entry pages and at the bottom of them, I have a div with the code below in it which randomly links to any entry.

I now would like this to disallow the entry that I am on from that randomization. I assume from other threads it would be something to do with finding the ID of the entry the user is on and not allowing it in the randomize?

{% for image in randomEntry.projectFeaturedImage %}

    <a href="{{ randomEntry.url }}">{{ randomEntry.title }}</a>
    <a href="{{ randomEntry.url }}">See Project</a>

{% endfor %}
2

Here are a couple of useful stackexchange questions

Putting that together:

{% set randomEntry = craft.entries({
  id: 'not '~entry.id,
  order: 'RAND()'
}).first %}

This will fetch entries whose id is not entry.id (entry is the current entry), ordered randomly, and then give you the first of those.

Probably you want to specify the section or sections to fetch your entries from:

{% set randomEntry = craft.entries({
  section: ['thisSection', 'thatSection', 'theOtherSection'],
  id: 'not '~entry.id,
  order: 'RAND()'
}).first %}

This will do the same thing, but only fetch entries from those 3 sections.

After all of that, you should also be sure to test that there actually is an entry:

{% if randomEntry %}
  ... do your thing ...
{% endif %}
| improve this answer | |
  • Awesome thank! I got most the way there using other threads but couldn't figure out how to add the " id: 'not '~entry.id " to what I had: {% set randomEntry = craft.entries.section('projects').order('RAND()').first() %} – leham789 Feb 26 '17 at 3:54

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.