12

Hopefully this is a simple question but I couldn't find a clear answer anywhere - I just want to be able to pull in data from a specific entry, where I know the identifier.

In this particular case, the entry is a single. Currently I'm doing this:

{% set entry = craft.entries.section('myChosenSection').first() %}

Is this correct, or is there a more concise way?

26

This is the correct way.

Examples:

Singles:

{% set entry = craft.entries.section('section_handle').first() %}

Channels / Structures:

{% set entry = craft.entries.section('section_handle').slug('my_slug').first() %}

General:

{% set entry = craft.entries.id(5).first() %}

See craft.entries for a list of all properties.

  • I would use {% set entry = craft.entries.section('myChosenSection').first() %} to get the first entry in a Channel or Sructure, and {% set entry = craft.entries.section('myChosenSection').find() %} to get the Single. – Andrea DeMers Jul 8 '14 at 13:12
  • Thanks Victor, it too seemed to me counter intuitive to have to use .first() in a single, however it seems if I remove it then the request returns nothing, as does using .find() - have I got something wrong in setup? – pumkincreative Jul 8 '14 at 14:49
  • 3
    Actually first() would still be necessary here. If you want to get an array of multiple entries back, it’s find() that’s not necessary, since find() is the default behavior. – Brandon Kelly Jul 8 '14 at 14:54
  • So for Single:{% set entry = craft.entries.section('myChosenSection').first() %}, and for Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').find() %} or {% set entry = craft.entries.section('myChosenSection') %}, and first item in Channel/Structure array: {% set entry = craft.entries.section('myChosenSection').first() %}? – Andrea DeMers Jul 8 '14 at 15:21
  • 4
    @VictorIn craft.entries doesn’t care whether it’s a Single or a Channel or a Structure. It’s only responsible for taking some parameters, passing them off to the ElementsService, and then either returning an array of all the results (find()) or just the first result (first()). You can technically use find() with a Single section if you want; you’ll just get an array back of that one entry. – Brandon Kelly Jul 8 '14 at 19:21
4

I realize this question was geared towards getting the entry via twig. Here is how you could accomplish the same from a plugin. In my case, I wanted to find an entry via slug.

MyPluginController.php

public function actionMyAction() {
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'mySectionHandle';
    $criteria->slug = $this->_getLastSegment(craft()->request->getUrlReferrer());
    ...

    $entries = $criteria->find();

    if ($entries) {
        foreach ($criteria as $entry) {
            error_log($entry->title);
        }
    } else {
        // No entry found that matches criteria.
    }
}

private function _getLastSegment($url)
{
    $path = parse_url($url, PHP_URL_PATH);
    $trimmed = trim($path, '/');
    $tokens = explode('/', $trimmed);

    return end($tokens);
}

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.