2

It appears that getting a entry that is a single is more tricky that it should be.

This works,

craft.entries.section('footer').first()

This does not work,

craft.entries.section('footer').limit(1)

This does not work,

craft.entries.section('footer')

Does any one know of any documentation that lays out the usage of when to use limit, nothing, and first()? Seems very confusing.

8

The first example you showed is the exact right way to do it.

The second & third examples you showed will technically work, but return an array of entries. Your single would be the only entry inside that array.

In your second & third examples, you're dealing with an ElementCriteriaModel. You can append several methods to an ECM to generate results from it...

  • first() - Returns an individual Entry (in this case your Single)
  • find() - Returns an array of matching entries
  • ids() - Returns an array of entry IDs
  • total() - Returns a number (total matches)
  • last() - Opposite of first()
  • nth() - Returns an individual Entry

Important to note... If you don't specify one of these methods, then find() will be automatically applied when it's used as an array.

  • Thanks for the detailed answer, and for providing additional context beyond the OP's question. If the 2nd & 3rd examples return an array, would craft.entries.section('footer')[0] work? – Nathan D Huening Feb 9 '17 at 21:04
  • @NathanDHuening: Might want to post that as a separate question. Feel free to link back to this thread for reference. – Lindsey D Feb 9 '17 at 21:31

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.