1

I can do the following to pull out the page title after parsing in just the page slug:

$page = 'about';
$entry = craft()->elements->getCriteria(ElementType::Entry)->first();
$entry->slug = $page;
echo $entry->title;

(I understand this is overkill, but an answer to this question will help me answer further questions I dare not try to explain here).

The problem is, the slug doesn't need to be unique if other sections/channels/singles use the same name, and the method above only calls out the first instance of "about".

How can I define what section is being referred to?

This will update the handle, but has no effect on the output:

$entry->section->handle = "pages";

This returns a Read Only error:

$entry->section = "Pages"

Property "Craft\EntryModel.section" is read only.

Mark Notton
  • 2,337
  • 1
  • 15
  • 30

1 Answers1

1

You've got a couple of incorrect things going on in your current example.

Calling craft()->elements->getCriteria(ElementType::Entry) returns an ElementCriteriaModel.

That's what you want to set the parameters on for the entries you're searching for. Stuart sums it up well here.

By calling ->first() on it immediately, you're going to get the defaults, which for entries is by post date in descending order.

By calling:

$entry->slug = $page;
echo $entry->title;

You are actually updating the slug for the entry that was returned to be something new.

This should work for what you're trying to do:

    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->slug = 'about';
    $criteria->section = 'news';
    $entry = $criteria->first();
    $title = $entry->title;
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • Thanks Brad [again]. I realised calling ->first() was the start of my problems, I realise now I overlooked some fairly basic formatting/syntax issues too. Works like a charm. – Mark Notton Aug 21 '15 at 08:15